清除多选启用

发布于 2024-08-21 06:55:20 字数 36 浏览 3 评论 0原文

我是否必须遍历所有并设置删除“选定”属性还是有更好的方法?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

并安 2024-08-28 06:55:20

只需在

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

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

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

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

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

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

$("#my_select option:selected").prop("selected", false);
孤者何惧 2024-08-28 06:55:20

为了清除所有选择,我正在使用这样的方法,它对我来说工作得很好。
这是脚本:

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

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

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' );
千年*琉璃梦 2024-08-28 06:55:20

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

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

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

$("#my_select").val('')
小霸王臭丫头 2024-08-28 06:55:20

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

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

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

 $("#drp_assign_list option[value]").remove();
自由如风 2024-08-28 06:55:20

在 JQuery 中:

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

In JQuery:

  $("#id option:selected").prop("selected", false);
  $("#id").multiselect('refresh');
心的位置 2024-08-28 06:55:20
$('.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>

$('.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>

七颜 2024-08-28 06:55:20

$("#ddlMultiselect").val('').trigger("change");

对于多选下拉菜单,它将清除显示的文本。

$("#ddlMultiselect").val('').trigger("change");

for multi-select drop-down, it will clear displayed text.

装纯掩盖桑 2024-08-28 06:55:20

jQuery 的 :selected 选择器可能就是您正在寻找的。

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

〃温暖了心ぐ 2024-08-28 06:55:20

在 javascript 中,

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

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

In 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.

for(var i=0;i<list.length;i++)
{
   if(list[i].Selected==true)
    {
       list[i].Selected=false;
    }
}
倒数 2024-08-28 06:55:20

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

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>

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 checked or selected
while those having a value that doesn't match one of the elements of
the array will be unchecked or unselected, depending on the type.

$('.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>

心意如水 2024-08-28 06:55:20
$('.selectpicker').selectpicker('deselectAll');

https://developer.snapappointments.com/bootstrap-select/methods/

$('.selectpicker').selectpicker('deselectAll');

https://developer.snapappointments.com/bootstrap-select/methods/

蓝天 2024-08-28 06:55:20

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

仅使用此方法即可清除下拉列表的选项和选择

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

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

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

因此,最终的代码是

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

欢迎提出建议和改进。

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.

书间行客 2024-08-28 06:55:20
$("#my_select option:selected").prop("selected", false).change();

其数值清晰,显示文字也清晰。

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

Its clear value and display text as well.

春庭雪 2024-08-28 06:55:20

假设您正在使用 David Stutz 的 Bootstrap 多选下拉菜单

$('#selectId').multiselect('deselectAll', false);

,但由于某种原因它在初始化方法中不起作用

Assuming you are using Bootstrap multiselect dropdown by David Stutz

$('#selectId').multiselect('deselectAll', false);

But for some reason it does not work inside initialization method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文