jQuery<选择>如果在多个 选择>
我有一个与这里提出的问题类似的问题: jQuery <选择>如果在其他
但是,我的有所不同,因为会有两个以上的选择框。在提供的答案中,当选择一个选项时,它会在其他选择框中设置为禁用(我希望发生这种情况),但我不希望在不同的选择框中选择的任何其他选项被禁用删除。
这有道理吗?
示例 html:
<div>
<select name="homepage_select_first">
<option>No Match</option>
<option value="1">Test</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
</div>
<div>
<select name="homepage_select_second">
<option>No Match</option>
<option value="1">Test</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
</div>
<div>
<select name="homepage_select_third">
<option>No Match</option>
<option value="1">Test</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
</div>
并且 jQuery:
$('select[name*="homepage_select"]').change(function() {
var value = $(this).val();
alert(value);
$('select[name*="homepage_select"]').children('option').each(function(){
if ( $(this).val() === value ) {
$(this).attr('disabled', true)//.siblings().removeAttr('disabled');
}
});
});
注释 .siblings().removeAttr('disabled')
根本不会删除禁用属性...但是,我只想在未选择它的情况下删除它在任一选择框中。
基本上,我只想一次在其中一个选项中选择一个选项。我认为如果我只能在刚刚更改的项目上 .removeAttr('disabled')
,我认为这会起作用。但是,我不知道该怎么做。
有人有什么想法吗?
谢谢!
I have a question similar to the one asked here: jQuery <select> option disabled if selected in other <select>
But, mine varies in that there will be more than two select boxes. In the answer provided, when an option is selected, it is set to disabled in the other select boxes (which I want to happen), but I don't want any other options selected in a different select box to have disabled removed.
Does that make sense?
Example html:
<div>
<select name="homepage_select_first">
<option>No Match</option>
<option value="1">Test</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
</div>
<div>
<select name="homepage_select_second">
<option>No Match</option>
<option value="1">Test</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
</div>
<div>
<select name="homepage_select_third">
<option>No Match</option>
<option value="1">Test</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
</div>
And the jQuery:
$('select[name*="homepage_select"]').change(function() {
var value = $(this).val();
alert(value);
$('select[name*="homepage_select"]').children('option').each(function(){
if ( $(this).val() === value ) {
$(this).attr('disabled', true)//.siblings().removeAttr('disabled');
}
});
});
Commenting out .siblings().removeAttr('disabled')
simply never removes the disabled attribute...but, I want to remove it ONLY if it isn't selected in any one of the select boxes.
Basically, I only want an option to be selected in one of the selects at a time. I would think that if I could only .removeAttr('disabled')
on the item that was just changed, I think that would work. But, I'm not sure how to go about this.
Does anyone have any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以做到。基本上按照您的评论要求进行操作 - 确保所有 3 个选择都有独特的选择。一旦在 1 个选择框中做出选择,该选项在其他框中就不再可用。
实例:http://jsfiddle.net/QKy4Y/
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.
Live example: http://jsfiddle.net/QKy4Y/
你可以这样做:
在这里小提琴: http://jsfiddle.net/8AcN4/2/
You could do:
fiddle here: http://jsfiddle.net/8AcN4/2/