简体   繁体   中英

remove specific items from dropdown list using jquery

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?

$("#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

这可以帮助您: - 使用jQuery删除选定的选项

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM