Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling <select> option based on other <select> option

Tags:

jquery

How to disable multiple options in select, in case that exact options are selected in other selects. I found answered question for problem similar to mine on this link: http://jsfiddle.net/dZqEu/

jQuery code looks next:

$(this).siblings('select').children('option').each(function() {
    if ( $(this).val() === value ) {
        $(this).attr('disabled', true).siblings().removeAttr('disabled');   
    }

The problem is that this works only for 2 selects. I need total 3 selects which should disable possibility that same values in selects are selected.

This code won't work for 3 selects: http://jsfiddle.net/dZqEu/968/

Tnx in advance

NOTE:

When the select1 is set to 1, select 2 is set to 2, i can't disable values 1&2 in select3. It only disables value 2 in select 3...

like image 800
Ivan Pandžić Avatar asked Dec 21 '22 06:12

Ivan Pandžić


2 Answers

try this:- http://jsfiddle.net/Z2yaG/

Using focus to get the prev value and remove disabled ones. In your html i have added value -1 for default option.

<option value="-1">No Match</option>

     var prev = -1;
$("select").change(function () {
    if ($(this).val() > -1) {
        $("select").not(this).find("option[value=" + $(this).val() + "]").attr('disabled', 'disabled');
    }
    $("select").not(this).find("option[value=" + previous + "]").removeAttr('disabled');
}).focus(function () {
    previous = $(this).val();
});
like image 52
PSL Avatar answered Dec 22 '22 18:12

PSL


This seems to work: http://jsfiddle.net/QLn9b/1/

It is using your original code:

$("select").change(function() {   
    $("select").not(this).find("option[value="+ $(this).val() + "]").attr('disabled', true);
});
like image 26
tymeJV Avatar answered Dec 22 '22 19:12

tymeJV