admin管理员组文章数量:1355679
I have n drop-downs like this:
<select id="select1">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<select id="select2">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
with identical options. All the choices should be unique, so once the option is selected in one bobox, it should be removed from others. So, if a user selects "1" in select1 there will be only options "2" and "3" in select2.
Now, jQuery disable SELECT options based on Radio selected (Need support for all browsers) offers a nice solution, but how can I modify this to work with selects instead of radio buttons?
I have n drop-downs like this:
<select id="select1">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<select id="select2">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
with identical options. All the choices should be unique, so once the option is selected in one bobox, it should be removed from others. So, if a user selects "1" in select1 there will be only options "2" and "3" in select2.
Now, jQuery disable SELECT options based on Radio selected (Need support for all browsers) offers a nice solution, but how can I modify this to work with selects instead of radio buttons?
Share Improve this question edited May 23, 2017 at 12:19 CommunityBot 11 silver badge asked Apr 2, 2010 at 12:02 Nikolay LapitskiyNikolay Lapitskiy 9751 gold badge10 silver badges14 bronze badges2 Answers
Reset to default 7Is this what you mean?
This would remove everything from #select2
that was in #select1
:
$("#select2 option").each(function() {
if($("#select1 option[value=" + this.value + "]").length)
$(this).remove();
});
Here's another alternative that removes all duplicates:
$("select").each(function() {
var select = $(this);
select.children("option").each(function() {
$('select').not(select).children("option[value=" + this.value + "]").remove();
});
});
This removes every duplicate, leaving he option only in the first <select>
it found it in.
Update for your ment:
$("#select1").change(function() {
$("#select2 option[value=" + $(this).val()+ "]").remove();
});
Alternatively, if the selects have the same options in the same order.
$('select').change(function() {
var selectedIndex = $(this).find(':selected').index();
$('select').not(this).find('option:nth-child(' + selectedIndex + ')').remove();
)};
I think this way is faster (but less easy to read).
本文标签: javascriptHow to remove options from select element using jqueryStack Overflow
版权声明:本文标题:javascript - How to remove options from select element using jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744011413a2575633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论