禁用 <选择>选项基于其他 <select>选项 [英] Disabling <select> option based on other <select> option
问题描述
如何在选择中禁用多个选项,以防在其他选择中选择了确切的选项.我在此链接上找到了与我的问题类似的问题的答案:http://jsfiddle.net/dZqEu/
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 代码:
jQuery code looks next:
$(this).siblings('select').children('option').each(function() {
if ( $(this).val() === value ) {
$(this).attr('disabled', true).siblings().removeAttr('disabled');
}
问题是这仅适用于 2 个选择.我需要总共 3 个选择,这应该禁用选择中相同值的可能性.
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.
此代码不适用于 3 个选择:http://jsfiddle.net/dZqEu/968/
This code won't work for 3 selects: http://jsfiddle.net/dZqEu/968/
提前交易
注意:
当 select1 设置为 1 时,select 2 设置为 2,我无法禁用 select3 中的值 1&2.它仅禁用选择 3 中的值 2...
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...
推荐答案
试试这个:- http://jsfiddle.net/Z2yaG/
使用焦点获取上一个值并删除禁用的值.在您的 html 中,我为默认选项添加了值 -1.
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();
});
这篇关于禁用 <选择>选项基于其他 <select>选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!