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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:34:50  来源:igfitidea点击:

JQuery Remove Select Option From List

javascriptjqueryselectoption

提问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

I have just set up a fiddlehere, and this works. Are you referencing the JQuerylibrary in the page? The only change in my code is replacing jquerywith $

我刚刚在fiddle这里设置了一个,这很有效。您是否JQuery在页面中引用图书馆?我的代码中唯一的变化是替换jquery$

$(document).ready(function () {
    $("div#test option[value=2014]").remove();
});

回答by Toni Michel Caubet

Try

尝试

jQuery('div#test option[value="2014"]').remove();

Or

或者

jQuery("div#test option:last-child").remove();

回答by Nadeemmnn Mohd

JQUERY

查询

$("#List option:selected").remove();

Fiddle Demo

小提琴演示

fiddle

小提琴