Disable option in dropdown list based on selection in other list

前端 未结 1 1489
借酒劲吻你
借酒劲吻你 2021-02-09 19:09

I have the following code

$(\'select\').on(\'change\', function(){
    $(\'select option\').prop(\"disabled\", false);
    $(\"select\").not(this).find(\"option[         


        
1条回答
  •  -上瘾入骨i
    2021-02-09 19:35

    This is pretty much the same as what you already had, except it loops over each select each time one changes, and disables the option selected in the other selects:

    var $selects = $('select');
    
    $selects.on('change', function() {
    
        // enable all options
        $selects.find('option').prop('disabled', false);
    
        // loop over each select, use its value to 
        // disable the options in the other selects
        $selects.each(function() {
           $selects.not(this)
                   .find('option[value="' + this.value + '"]')
                   .prop('disabled', true); 
        });
    
    });
    

    Here's a fiddle

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