jQuery JQuery从列表中删除选择选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19771681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery Remove Select Option From List
提问by user1172854
I have the following select box (I am not able to add a class or ID to the select form which is why I wrapped it in a div):
我有以下选择框(我无法向选择表单添加类或 ID,这就是我将其包装在 div 中的原因):
<div id="test"><select name="wpv-yr">
<option value="0" selected="selected">All Years</option>
<option value="2010">2010 (6)</option>
<option value="2011">2011 (12)</option>
<option value="2012">2012 (32)</option>
<option value="2013">2013 (129)</option>
<option value="2014">2014 (24)</option>
</select></div>
And I don't want to show the last option, 2014, in the drop down list. I've tried adding:
我不想在下拉列表中显示最后一个选项 2014。我试过添加:
jQuery(document).ready(function() {
jQuery("div#test option[value=2014]").remove();
});
I've also tried single quotes around the value:
我也试过在值周围加上单引号:
jQuery(document).ready(function() {
jQuery("div#test option[value='2014']").remove();
});
But that didn't work either. The 2014 option is still appearing in the list.
但这也不起作用。2014 年的选项仍然出现在列表中。
Where am I going wrong?
我哪里错了?
回答by christiandev
回答by Toni Michel Caubet
Try
尝试
jQuery('div#test option[value="2014"]').remove();
Or
或者
jQuery("div#test option:last-child").remove();