隐藏所选的optgroups及其内容,除了所选内容之外 [英] Hide select's optgroups and their contents except the one selected
本文介绍了隐藏所选的optgroups及其内容,除了所选内容之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为国家创建一个选择标签,当我们选择国家时,它会隐藏所有选择群组及其选择标签的内容,除了optgroup及其具有与所选国家/地区标签相同的标签的内容,例如
< select name =country>
< option value =美国>美国< / option> <! - 如果我选择US - >
< option value =Canada>加拿大< / option>
< option value =墨西哥>墨西哥< / option>
< / select>
< optgroup label =美国>
< option value =California>加利福尼亚< / option> <! - 只有这些状态 - >
< option value =纽约>纽约< / option> <! - 显示 - >
< / optgroup>
< optgroup label =Canada> <! - hidden - >
< option value =Ontario>安大略省< / option> <! - hidden - >
< option value =Quebec> Quebec< / option> <! - hidden - >
< / optgroup>
< optgroup label =墨西哥> <! - hidden - >
< option value =Baja California> Baja California< / option> <! - hidden - >
< option value =墨西哥Estado>墨西哥Estado< / option> <! - hidden - >
< / optgroup>
< / select>
感谢您的帮助
解决方案
'):eq(0)).on(change,function(){
$(select [name ='country']:eq(1))
.find( optgroup,option)
.hide()
.filter([label ='+ this.value +'],[label ='+ this.value +']> ; *)
.show();
});
DEMO: http://jsfiddle.net/MVkXg/
I want to create a select tag for countries that when we select the country it hides all optgroups and their contents of a select tag for states except the optgroup and its content which has the same label as the selected country option, example
<select name="country">
<option value="United States">United States</option> <!-- if I select US -->
<option value="Canada">Canada</option>
<option value="Mexico">Mexico</option>
</select>
<select name="country">
<optgroup label="United States">
<option value="California">California</option> <!-- Only these states -->
<option value="New York">New York</option> <!-- are displayed -->
</optgroup>
<optgroup label="Canada"> <!-- hidden-->
<option value="Ontario">Ontario</option> <!-- hidden-->
<option value="Quebec">Quebec</option> <!-- hidden-->
</optgroup>
<optgroup label="Mexico"> <!-- hidden-->
<option value="Baja California">Baja California</option> <!-- hidden-->
<option value="Mexico Estado">Mexico Estado</option> <!-- hidden-->
</optgroup>
</select>
Thanks for help
解决方案
This seems to be working fine for me:
$("select[name='country']:eq(0)").on("change", function() {
$("select[name='country']:eq(1)")
.find("optgroup,option")
.hide()
.filter("[label='" + this.value + "'],[label='" + this.value + "'] > *")
.show();
});
DEMO: http://jsfiddle.net/MVkXg/
这篇关于隐藏所选的optgroups及其内容,除了所选内容之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文