简体   繁体   English

如何使用jQuery清除SELECT输入中的所有选定项目?

[英]How to clear all selected items in a SELECT input using jQuery?

I am think this should be fairly easy, but struggling a bit ... I have a SELECT input element that allows the user to select multiple items.我认为这应该相当容易,但有点挣扎......我有一个SELECT输入元素,允许用户选择多个项目。 I would like to provide a mechanism for them to clear all selected items, probably will just be a SPAN tag or something similar.我想为他们提供一种机制来清除所有选定的项目,可能只是一个SPAN标签或类似的东西。

Anyway ... using jQuery, how I would I clear the selection on the SELECT input element so no items are selected.无论如何......使用jQuery,我将如何清除SELECT输入元素上的SELECT以便没有选择任何项目。

In the case of a <select multiple> the .val() function takes/returns an array, so you can simply pass in an empty array to clear the selection, like this:<select multiple>的情况下, .val()函数接受/返回一个数组,因此您可以简单地传入一个空数组来清除选择,如下所示:

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

You can test it out here .你可以在这里测试一下

This worked to clear all selected options for me..这有助于为我清除所有选定的选项。

$("#selectListName").prop('selectedIndex', -1)

The select list looked like选择列表看起来像

<select multiple='multiple' id='selectListName'> 
  <option>1</option> 
  <option>2</option> 
  <option>3</option> 
  <option>4</option>
</select>

Try: // Just put # before select to fix this.尝试: // 只需在选择之前放置 # 即可解决此问题。 Works perfect.工作完美。 $("#select option:selected").each(function () { $(this).remove(); //or whatever else }); $("#select option:selected").each(function () { $(this).remove(); // 或其他任何 });

只有这对我有用:-

$("#selectid").find('option').attr("selected",false) ;

This solution worked for me...这个解决方案对我有用......

$('#my-Select').val('').trigger('change');

This works on almost any input type.这适用于几乎所有输入类型。

我尝试类似的东西

$("#my-Select").find('option').attr("selected","") ;

Try this尝试这个

<select id='select1' ></select>
<input type= 'button' value = 'Clear Selection' id = 'remove' />

$(document).ready(function() {  
   $('#remove').click(function() {  
     return $('#select1 option:selected').remove(); 
   });
 });
Maybe 

$('#mySelect option').each(function(i, e)
{
   e.selected = false
});

Try that:试试看:

$("#selectID").empty();

Worked for me!为我工作!

Better than this, you can use:比这更好,您可以使用:

$("#selectID").empty();

That works on almost anything in the DOM.这几乎适用于 DOM 中的任何内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何清除 dropdpwn 中的所有选定项目? - How to clear all selected items in a dropdpwn? 使用Jquery,如何在当前选择的输入元素之后选择所有输入文本元素? - Using Jquery how do I select all input text elements after currently selected input element? 清除Select2的所有列表项,而不是选定的 - Clear all of the list items of Select2 rather than the selected 在从动态附加的li列表中选择时,将选定的li文本附加到输入中,并使用jquery清除列表 - on select from dynamically appended li list append the selected li text to input and also clear the list using jquery 如何使用jQuery清除输入数组的所有元素 - How to clear a input array all elements using jQuery 如何使用jquery检查所有选择框是否都选择了选项? - How to check if all select boxes has selected option using jquery? 移动浏览器中的jQuery .Select()将自动清除所有输入文本 - Jquery .Select() in mobile browser will auto clear all input text 如何使用 jQuery Selector 选择列表中的所有项目 - How to select all items in a list using jQuery Selector 当我使用select2 jquery选择&#39;ALL&#39;项目时,如何禁用选择框中的所有项目 - How to disable all items in select box when i am selecting 'ALL' item using select2 jquery 如何使用 JavaScript 或 Jquery 选择在 GridView 中选择的行项的子项? - How to select the sub-items of a row item selected in GridView by using JavaScript or Jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM