JQuery选项在从多选框中选择时删除

JQuery option remove on select from multiple select box

本文关键字:选择 删除 选项 JQuery      更新时间:2023-09-26

我是JQuery的新手。我一直在寻找解决方案,但找不到任何解决方案。我有3个选择框,在第一个选择框中选择的选项需要从接下来的两个选择框删除。我有一个代码,它实际上隐藏了FF和chrome中的选定选项。但在IE中,它并没有被隐藏。此外,如果用户改变主意,并在第一个选择框中重新选择他已经选择的选项,我需要带回选定的选项。有人能帮我吗。非常感谢您的帮助。

var array= [];
$(document).ready(function(){
    $('select').on('change', function(event ) {
        $('select').not(this).find('option').show();
        var value = $(this).val();
        //alert(value);
        array.push(value);
        for (var i = 0; i < array.length; i++) {
            // Here i am disabling the option as well as hiding. Because in IE hide does not work.
            $('select').not(this).find('option[value="'+array[i]+'"]').attr('disabled','disabled').hide();
        }
    });
});

试试这个,

$("#first_select").change(function(){
    $("#second_select option,#third_select option").removeAttr("disabled");
    $.each($("#first_select").val(), function( index, value ) {
       $("#second_select option[value="+value+"]").attr("disabled","disabled");
       $("#third_select option[value="+value+"]").attr("disabled","disabled");
    });      
});

http://jsfiddle.net/anunaydahal/h8n1agdp/11/