如何从下拉列表中删除所选项目(使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:25:03  来源:igfitidea点击:

How to remove a selected item from a dropdown list (Using Jquery)

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

Use the eqselector.

使用eq选择器。

var index = $('#cmbTaxID').get(0).selectedIndex;
$('#cmbTaxID option:eq(' + index + ')').remove();

This is the best way to do it because it's index-based, not arbitrary value-based.

这是最好的方法,因为它是基于索引的,而不是基于任意值的。

回答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 3with 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();

})