jQuery - hide option element by value

久未见 提交于 2019-12-10 17:44:04

问题


Trying to hide an option element from a list. I have tried several methods however, cannot seems to work. I need to call select by name as I cannot add ID or class.

$('select[name*="boxes"] option[value="box6"]').hide();

here the fiddle: http://jsfiddle.net/q2nb08t6/

any suggestions?


回答1:


If you can go with removing it, this seems to work in all browsers:

$('select[name*="boxes"] option[value="box6"]').remove();

Tested in FF, Chrome, Safari, IE and Opera.

Demo Fiddle




回答2:


Change your selector to

$('select[name*="boxes"] > option[value="box6"]').hide();

The > means to search the preceding selector for the following selector.

Here's your updated fiddle: http://jsfiddle.net/q2nb08t6/2/

Also, if this is a one-off situation, you could simplify your selector even further to:

$('option[value="box6"]').hide();


来源:https://stackoverflow.com/questions/30762709/jquery-hide-option-element-by-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!