2013-04-25 151 views
4

如何在select中禁用多个选项,以防在其他选择中选择确切选项。我发现回答问题相似开采这个环节上的问题: http://jsfiddle.net/dZqEu/根据其他<select>选项禁用<select>选项

jQuery代码看起来未来:

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

的问题是,这仅适用于2种选择。我需要总共3个选择,它应该禁用选择相同值的可能性。预先

http://jsfiddle.net/dZqEu/968/

TNX:

该代码将不用于3个选择工作

当SELECT1被设置为1,选择2被设置为2,则i可以在select3中禁用值1 & 2。它仅禁止值2选择3 ...

+0

你的代码被禁止用于*所有选择所有选择中选择的值,*。你的问题到底是什么? – Matt 2013-04-25 16:43:22

+0

看看3选择的例子。当select1设置为1时,选择2设置为2,我不能禁用select3中的值1和2。它仅在选择3时禁用值2 ... – 2013-04-25 16:46:47

+0

您应该在您的小提琴中启用jQuery。 – tymeJV 2013-04-25 16:47:48

回答

7

试试这个: - http://jsfiddle.net/Z2yaG/

使用对焦获取prev值并删除禁用的值。 在你的html中,我为默认选项增加了值-1。

<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(); 
}); 
+0

jQuery版本在http://jsfiddle.net/dZqEu/968/是1.4.4 – hjpotter92 2013-04-25 17:06:06

+0

更新小提琴链接与jquery1.4.4 ...感谢指出..我注意到。 – PSL 2013-04-25 17:09:15

+0

免责声明:我改进了@tymejv回答 – PSL 2013-04-25 17:33:08

2

这似乎工作:http://jsfiddle.net/QLn9b/1/

它是利用原来的代码:

$("select").change(function() { 
    $("select").not(this).find("option[value="+ $(this).val() + "]").attr('disabled', true); 
}); 
+0

这是我需要的,但代码应该删除属性禁用,当它不再被选中。 .. – 2013-04-25 16:58:53

+0

+1,因为我的解决方案是您的方法的改进版本。 – PSL 2013-04-25 19:57:08

+0

不错的完成它!今天得到了其他东西包装回来:\ – tymeJV 2013-04-25 19:58:13

0

试试这个。

$('select').change(function() 
{  
    var value = $(this).val(); 
    $('option').removeAttr("disabled"); 
    $('select').each(function() 
    { 
     $("option[value=" + $(this).val() + "]").attr("disabled","disabled"); 
    }); 
}); 
1

我认为这是你在找什么。

$('select').change(function() { 

    var select_elements = $('select') 
    $.each(select_elements, function(i,elem){ 
    var sel_values = []; 

    // Selecting and Iterating on Sibling Selects   
    var siblis = $(elem).siblings('select') 
    $.each(siblis, function(i, sibli){ 
     if($(sibli).val()!='No Match'){ 
      sel_values.push($(sibli).val()); 
     } 
    }); 

    // Iterating on Options of Select 
    $(this).children('option').each(function() { 
     $(this).removeAttr('disabled'); 

     if($.inArray($(this).val(), sel_values) !== -1){ 
      $(this).attr('disabled', true); 
     } 
    });   

    });  
}); 

如果所有选择具有从1至6的值,

If select1 value  --> 1, 
and select2 value  --> 3 (1 is disabled) 
then in select3 value --> 5 (1,3 are disabled) 

选择选择3后,

In select1 --> 1 (3,5 disabled) 
In select2 --> 3 (1,5 disabled) 
In select3 --> 5 (1,3 disabled) 

这里是的jsfiddle:http://jsfiddle.net/prem_nagalla1/rY4n3/

希望它能帮助: )

1

这是一种方法。当任何下拉改变时,计算所有选定的值(第一个除外)。如果其他框的选定值出现在值列表中,请禁用相应的选项。

$('select').change(function() { 
    var values = [], 
    others = $(this).siblings('select'), 
    all = others.andSelf(); 

    all.each(function() { 
     if (this.selectedIndex > 0) { 
      values.push(this.options[this.selectedIndex].value); 
     } 
    }); 

    others.each(function() { 
     for (var i = 0; i < this.options.length; ++i) { 
      this.options[i].disabled = $.inArray(this.options[i].value, values) !== -1; 
     } 
    }); 
}); 

Demo

相关问题