javascript 使用 jquery 从下拉列表中删除特定项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5778237/
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-10-25 18:18:34  来源:igfitidea点击:

remove specific items from dropdown list using jquery

javascriptjqueryhtml

提问by john

I have a multi select dropdown list. I can get the array of selected values using:

我有一个多选下拉列表。我可以使用以下方法获取选定值的数组:

selectedItems = $("#myList").val(); // works.

selectedItems = $("#myList").val(); // works.

Now, how can I remove the selected items from the dropdown list?

现在,如何从下拉列表中删除所选项目?

回答by Felix Kling

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

will work.

会工作



Edit: I misunderstood the comment, but I will leave it as an example for removing certain elements in general.
If you want to remove the elements based on the value in the array, you have to loop over the array:

编辑:我误解了评论,但我会将其作为删除某些元素的示例。
如果要根据数组中的值删除元素,则必须遍历数组:

var $list = $("#myList"),
    toRemove = $();

for(var i = selectedItems.length; i--;) {
   toRemove = toRemove.add($list.find('option[value="' + selectedItems[i] + '"]'));
}
toRemove.remove();

DEMO

演示

回答by Misam

回答by Genius

$("[Id$='ddlShowRun'] option:selected").remove();