jQuery - disable selected options

前端 未结 4 1255
心在旅途
心在旅途 2020-12-01 05:13

Need to disable already selected options in select box using jQuery. I\'d like it to grey out like asmselect.

Test my example here.

//JS
$(\"#theSele         


        
相关标签:
4条回答
  • 2020-12-01 05:24

    pls try this,

    $('#select_id option[value="'+value+'"]').attr("disabled", true);
    
    0 讨论(0)
  • 2020-12-01 05:28

    Add this line to your change event handler

        $("#theSelect option:selected").attr('disabled','disabled')
            .siblings().removeAttr('disabled');
    

    This will disable the selected option, and enable any previously disabled options.

    EDIT:

    If you did not want to re-enable the previous ones, just remove this part of the line:

            .siblings().removeAttr('disabled');
    

    EDIT:

    http://jsfiddle.net/pd5Nk/1/

    To re-enable when you click remove, add this to your click handler.

    $("#theSelect option[value=" + value + "]").removeAttr('disabled');
    
    0 讨论(0)
  • 2020-12-01 05:41

    This will disable/enable the options when you select/remove them, respectively.

    $("#theSelect").change(function(){          
        var value = $(this).val();
        if (value === '') return;
        var theDiv = $(".is" + value);
    
        var option = $("option[value='" + value + "']", this);
        option.attr("disabled","disabled");
    
        theDiv.slideDown().removeClass("hidden");
        theDiv.find('a').data("option",option);
    });
    
    
    $("div a.remove").click(function () {     
        $(this).parent().slideUp(function() { $(this).addClass("hidden"); });
        $(this).data("option").removeAttr('disabled');
    });
    

    Demo: http://jsfiddle.net/AaXkd/

    0 讨论(0)
  • 2020-12-01 05:46

    This seems to work:

    $("#theSelect").change(function(){          
        var value = $("#theSelect option:selected").val();
        var theDiv = $(".is" + value);
    
        theDiv.slideDown().removeClass("hidden");
        //Add this...
        $("#theSelect option:selected").attr('disabled', 'disabled');
    });
    
    
    $("div a.remove").click(function () {     
        $(this).parent().slideUp(function() { $(this).addClass("hidden"); });
        //...and this.
        $("#theSelect option:disabled").removeAttr('disabled');
    });
    
    0 讨论(0)
提交回复
热议问题