我这里有个情况,
使用 JQuery,我在下拉列表中附加了一些值。即使是在下拉列表中附加的一个或两个值。
对于添加,
tableui+='<option value="">'+resourceadd+'</option>';
$('#resourcess').append(tableui);
当页面自动重新加载存储在数据库中的值。
例如在下拉列表中添加了 4 个值(值未存储在数据库中),我想删除最后一个值。我在用,
$("#resourcess :last-child").remove();
同样的情况,我想删除两个值的中间值,怎么办??
最佳答案
假设您知道要删除的选项
的值,您可以使用filter
:
$('#resources option').filter(function() {
return this.value == 'foo'; // insert your value here.
}).remove();
或者一个属性选择器:
$('#resources option[value="foo"]').remove();
如果您不知道该值,但知道 option
在选择中的位置,您可以使用 eq()
按索引删除它:
$('#resources option').eq(1).remove(); // remove the 2nd option
关于javascript - 使用 jquery 删除下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28019894/