如何从下拉列表中删除所选项目(使用 Jquery)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3413188/
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
How to remove a selected item from a dropdown list (Using Jquery)
提问by Biki
How to remove one or more selected items in option tag, from a HTML dropdown list (Using Jquery).
如何从 HTML 下拉列表中删除选项标签中的一个或多个选定项目(使用 Jquery)。
For removing entire options from a combo box we can use the below Jquery statement.
要从组合框中删除整个选项,我们可以使用以下 Jquery 语句。
$("#cmbTaxIds >option").remove();
$("#cmbTaxIds >option").remove();
Assuming the below HTML code in aspx file.
假设 aspx 文件中有以下 HTML 代码。
<select id="cmbTaxID" name="cmbTaxID" style="width: 136px; display: none" tabindex="10" disabled="disabled">
<option value="0"></option>
<option value="3"></option>
<option value="1"></option>
</select>
If I want to remove only the middle value, then what should be the syntax for the same (using Jquery)?
如果我只想删除中间值,那么相同的语法应该是什么(使用 Jquery)?
回答by Jacob Relkin
回答by Etienne Dupuis
To remove the selected item:
要删除所选项目:
$("#cmbTaxID :selected").remove();
回答by VeeWee
something like this:
像这样:
$('#cmbTaxID option:selected').remove();
or even shorter:
甚至更短:
$('#cmbTaxID :selected').remove();
回答by Senseful
$("#cmbTaxIds >option[value='3']").remove();
Just replace 3
with the value of the element you want to remove.
只需替换3
为要删除的元素的值即可。
回答by Kristoffer Sall-Storgaard
A more generic answer to remove the selected option could be
删除所选选项的更通用的答案可能是
$('#somebutton').click(function(){
var optionval = $('#cmbTaxIds').val();
$('#cmbTaxIds > option[value=' + optionval + ']').remove();
})