How to prevent duplicate values in multiple dropdowns using jQuery

后端 未结 1 1769
情书的邮戳
情书的邮戳 2021-01-17 02:39

I have 5 dropdowns menu for users to select their preferences. All the dropdowns have the same choices. If the user has chosen a value for dropdown 1, that option should not

相关标签:
1条回答
  • 2021-01-17 03:38

    Instead of changing the status of the option right away, first you need to get current values in the select fields, so that you can disable them in other select fields

    demo

     $(".go").change(function(){
        var selVal=[];
        $(".go").each(function(){
            selVal.push(this.value);
        });
    
        $(this).siblings(".go").find("option").removeAttr("disabled").filter(function(){
           var a=$(this).parent("select").val();
           return (($.inArray(this.value, selVal) > -1) && (this.value!=a))
        }).attr("disabled","disabled");
    });
    
    $(".go").eq(0).trigger('change');
    


    the above code can be used with any number of select boxes :)

    0 讨论(0)
提交回复
热议问题