从 jQuery 的多选下拉列表中取消选择所有元素

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

Deselecting all elements from a multi-select dropdown in jQuery

jqueryclearmulti-select

提问by Fero

I have a multi-select drop down as following, where I have selected the options "Test 2" and "Test 3".

我有一个多选下拉菜单,如下所示,其中我选择了“测试 2”和“测试 3”选项。

<select id="edit-rec" class="form-select" multiple="multiple" name="rec[]">
<option value="6012">Test 1</option>
<option value="8436">Test 2</option>
<option value="4689">Test 3</option>
<option value="6784">Test 4</option>
</select>

I have a button called "Deselect All". When this button is clicked, all selected items should be deselected. In this case, the items I previously selected, "Test 2" and "Test 3", should now become deselected.

我有一个名为“取消全选”的按钮。单击此按钮时,应取消选择所有选定的项目。在这种情况下,我之前选择的项目“测试 2”和“测试 3”现在应该取消选择。

How can I accomplish this using jQuery?

如何使用 jQuery 完成此操作?

回答by Chuck Norris

$("#edit-rec option:selected").removeAttr("selected");

回答by ipr101

Try -

尝试 -

$("#edit-rec > option").attr("selected",false);

Demo - http://jsfiddle.net/LhSBu/

演示 - http://jsfiddle.net/LhSBu/

回答by M S

On click of radio button you can use this

单击单选按钮后,您可以使用它

$("#edit-rec  option").each(function(){

    this.selected=false;

});

回答by SPnL

It will remove all checked options from multiselect dropdownlist :

它将从多选下拉列表中删除所有选中的选项:

$('#ddlTradeShow').multiselect("clearSelection");

$('#ddlTradeShow').multiselect("clearSelection");

回答by Haim Evgi

$("#edit-rec option:selected").removeAttr("selected");

回答by Chris

Can do something like this JS Fiddle for setting up the click on the radio button http://jsfiddle.net/x5ck3/

可以做这样的事情 JS Fiddle 设置单击单选按钮 http://jsfiddle.net/x5ck3/

$('#rdClear').click(

function() {
    $("#edit-rec option:selected").removeAttr("selected");
});

回答by sunpietro

$("#butt").click(function () {
    $("#edit-rec > option").removeProp("selected");
});

that's correct with new jQuery version

这对新的 jQuery 版本是正确的

回答by Maykonn

The easiest way that I found to unselect all options in a multi-select dropdown was using .val([]).

我发现在多选下拉列表中取消选择所有选项的最简单方法是使用.val([]).

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

回答by Jean-Loup Becquet

There is now another possibility directly provided by the API :

现在 API 直接提供了另一种可能性:

$('#edit-rec').multiSelect('deselect_all');

Works fine, here you can find more options about it : http://loudev.com/

工作正常,在这里你可以找到更多关于它的选项:http: //loudev.com/