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

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

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

jquerymulti-select

提问by MetaGuru

Do I have to iterate through ALL the and set remove the 'selected' attribute or is there a better way?

我是否必须遍历所有并设置删除 'selected' 属性或者有更好的方法吗?

回答by meagar

Simply find all the selected <option>tags within your <select>and remove the selectedattribute:

只需找到您中的所有选定<option>标签<select>并删除该selected属性:

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

As of jQuery 1.6, you should use .propinstead of removing the attribute:

从 jQuery 1.6 开始,您应该使用.prop而不是删除属性:

$("#my_select option:selected").prop("selected", false);

回答by Sumit

In order to clear all selection, I am using like this and its working fine for me. here is the script:

为了清除所有选择,我正在使用它并且它对我来说很好用。这是脚本:

 $("#ddlMultiselect").multiselect("clearSelection");

 $("#ddlMultiselect").multiselect( 'refresh' );

回答by Prateek

Shortest and quickest way to remove all the selection, is by passing empty string in jQuery .val() function :

删除所有选择的最短和最快的方法是在 jQuery .val() 函数中传递空字符串:

$("#my_select").val('')

回答by Abdul Ershad

This is the best way to clear a multi-select or list box:

这是清除多选或列表框的最佳方法:

 $("#drp_assign_list option[value]").remove();

回答by Anand

In JQuery:

在 JQuery 中:

  $("#id option:selected").prop("selected", false);
  $("#id").multiselect('refresh');

回答by tDo

jQuery's :selectedselector is probably what you are looking for.

jQuery 的:selected选择器可能正是您要找的。

回答by Suraj Kumar

In javascript,

在 JavaScript 中,

You can use the list of all options of multiselect dropdown which will be an Array.Then loop through it to make Selected attributes as false in each Objects.

您可以使用多选下拉列表的所有选项列表,该列表将是一个数组。然后循环遍历它以使每个对象中的 Selected 属性为 false。

for(var i=0;i<list.length;i++)
{
   if(list[i].Selected==true)
    {
       list[i].Selected=false;
    }
}

回答by Arup Rakshit

You can pass an empty array to unselect all the selection. Here goes an example. From documentation

您可以传递一个空数组来取消选择所有选择。这是一个例子。从文档

val()allows you to pass an array of element values. This is useful when working on a jQuery object containing elements like , , and s inside of a . In this case, the inputs and the options having a value that matches one of the elements of the array will be checkedor selectedwhile those having a value that doesn't match one of the elements of the array will be uncheckedor unselected, depending on the type.

val()允许您传递元素值数组。这在处理包含 、 和 s 等元素的 jQuery 对象时很有用。在这种情况下,将选中选择具有与数组元素之一匹配的值的输入和选项,而具有与数组元素之一不匹配的值的输入和选项将被取消选中取消选中,具体取决于在类型上。

$('.clearAll').on('click', function() {
  $('.dropdown').val([]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select class="dropdown" multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

<button class='clearAll'>Clear All Selection</button>

回答by arbaz diwan

I've tried many solutions and came up with this.

我尝试了很多解决方案并提出了这个。

Options and selections of the dropdown are cleared using this only

仅使用此选项清除下拉列表的选项和选择

$("#my-multi option:selected").prop("selected", false);
$("#my-multi option").remove();


But the interface is not updated. So you have to do this


但是界面没有更新。所以你必须这样做

$('#my-multi').multiselect('rebuild');


Hence, the final code is


因此,最终的代码是

$("#my-multi option:selected").prop("selected", false);
$("#my-multi option").remove();
$('#my-multi').multiselect('rebuild');


Suggestions and improvements are welcome.


欢迎提出建议和改进。