jQuery <select> option disabled if selected in multiple <select> boxes

后端 未结 2 1520
闹比i
闹比i 2020-12-12 07:27

I have a question similar to the one asked here: jQuery

But, mine varies in that there will be mor

2条回答
  •  -上瘾入骨i
    2020-12-12 07:58

    This should do it. Basically does what your comments ask - ensures that all 3 selects have unique selections. Once a selection is made in 1 selext box that option is no longer available in the others.

    $('select[name*="homepage_select"]').change(function(){
    
    
        // start by setting everything to enabled
        $('select[name*="homepage_select"] option').attr('disabled',false);
    
        // loop each select and set the selected value to disabled in all other selects
        $('select[name*="homepage_select"]').each(function(){
            var $this = $(this);
            $('select[name*="homepage_select"]').not($this).find('option').each(function(){
               if($(this).attr('value') == $this.val())
                   $(this).attr('disabled',true);
            });
        });
    
    });
    

    Live example: http://jsfiddle.net/QKy4Y/

提交回复
热议问题