如何使用 jquery 清除多选下拉列表

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

How to clear a multiple select dropdown using jquery

jquery

提问by Satya

Possible Duplicate:
Quick way to clear all selections on a multiselect enabled <select> with jQuery?

可能的重复:
使用 jQuery 清除启用多选的 <select> 上的所有选择的快速方法?

Dear All.

各位。

I have a problem. once i have a multiselect box and i want to clear all the contents of that when i clickon the reset button.

我有个问题。一旦我有一个多选框,当我点击重置按钮时,我想清除其中的所有内容。

could you please provide me jquery for that?

你能为我提供jquery吗?

Thanks.

谢谢。

Satya

萨蒂亚

回答by Naftali aka Neal

I assume you mean clear selection:

我假设你的意思是明确的选择:

//for jQuery >= 1.6
$('select option').removeProp('selected');
//for jQuery < 1.6
$('select option').removeAttr('selected');

回答by Alnitak

To unselect all items if using jQuery 1.6.1 or later:

如果使用 jQuery 1.6.1 或更高版本,要取消选择所有项目:

$('#mySelect').children().removeProp('selected');

or for backwards compatibilty:

或为了向后兼容:

$('#mySelect').children().removeAttr('selected');

For simplicity's sake I'm assuming that your HTML is well formed and that your selectonly has optionchildren.

为简单起见,我假设您的 HTML 格式良好并且您select只有option孩子。

Also, if the list is large and performance is an issue, filterthe list so that you only change the ones that are actually selected - it's arguable whether it's simpler to simply deselect every option or only change the ones that need deselecting:

此外,如果列表很大并且性能是一个问题,filter那么列表使您只能更改实际选择的选项 - 简单地取消选择每个选项或仅更改需要取消选择的选项是否更简单,这是有争议的:

$('#mySelect').children(':selected').removeProp('selected');

[NB: using :selectedin the second function call is (according to the jQuery docs) more efficient than making a single compound selector]

[注意::selected在第二个函数调用中使用(根据 jQuery 文档)比制作单个复合选择器更有效]

回答by matt

In the future, you should post what you've tried so far - It gives people trying to answer your question a little more insight into what path you're taking, and where you might be going down the wrong path.

将来,您应该发布到目前为止您尝试过的内容 - 它让试图回答您的问题的人们更深入地了解您正在采取的道路以及您可能会走错路的地方。

That being said, you can remove a list item from it's container with the .remove()method, like this:

话虽如此,您可以使用该.remove()方法从其容器中删除列表项,如下所示:

$('#list option').each(function(index, option) {
    $(option).remove();
});

you can see it in action here: http://jsfiddle.net/y9pzS/

你可以在这里看到它的实际效果:http: //jsfiddle.net/y9pzS/

and here's a version that uses the .empty()method to remove all of child items in the list: http://jsfiddle.net/y9pzS/1/

这是一个使用该.empty()方法删除列表中所有子项的版本:http: //jsfiddle.net/y9pzS/1/

Note: there's a fair amount of ambiguity in your question. Not sure if you're looking to clear out the entire container, or just remove the selected items. This will remove allitems from the select box.

注意:您的问题中有相当多的歧义。不确定您是要清除整个容器,还是只是删除选定的项目。这将从选择框中删除所有项目。

回答by Ryan

See this SO post:

请参阅此 SO 帖子

$("#my_select option:selected").removeAttr("selected");