如何从下拉列表中删除所选项(使用Jquery)

Bik*_*iki 9 jquery

如何从HTML下拉列表中删除选项标记中的一个或多个所选项(使用Jquery).

要从组合框中删除整个选项,我们可以使用下面的Jquery语句.

$("#cmbTaxIds> option").remove();

假设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>
Run Code Online (Sandbox Code Playgroud)

如果我只想删除中间值,那么它的语法应该是什么(使用Jquery)?

Jac*_*kin 16

使用eq选择器.

var index = $('#cmbTaxID').get(0).selectedIndex;
$('#cmbTaxID option:eq(' + index + ')').remove();
Run Code Online (Sandbox Code Playgroud)

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


Eti*_*uis 13

要删除所选项目:

$("#cmbTaxID :selected").remove();
Run Code Online (Sandbox Code Playgroud)


Vee*_*Wee 5

这样的事情:

$('#cmbTaxID option:selected').remove();
Run Code Online (Sandbox Code Playgroud)

甚至更短:

$('#cmbTaxID :selected').remove();
Run Code Online (Sandbox Code Playgroud)