你如何使用 jquery 从列表框中取消选择所有项目

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

how do you unselect all items from a listbox using jquery

jquerylistbox

提问by leora

is there a way to unselect all items of a listbox using jquery without looping through each item?

有没有办法使用 jquery 取消选择列表框的所有项目而不遍历每个项目?

回答by Nick Craver

The shortest way is this method:

最短的方法是这种方法:

$("#myListBox").val([]);

This sets the value to an empty array, meaning select no values. .val()takes an array in the case of a <select multiple>element. Note that $("select").val('');???????also works here :)

这会将值设置为空数组,这意味着不选择任何值。 .val()<select multiple>元素的情况下采用数组。请注意,这$("select").val('');???????也适用于此处:)

回答by Matti Virkkunen

jQuery is designed to work with multiple elements at the same time:

jQuery 旨在同时处理多个元素:

$(listboxSelector).find("option").attr("selected", false);

回答by peter

$('#myListbox option').attr('selected',false);

回答by Levi Hackwith

$("#mylistbox").options.attr("selected", false);