开发者

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

开发者 https://www.devze.com 2023-02-06 06:35 出处:网络
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. I would like to provide a mechanism for them to clear all s

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. I would like to provide a mechanism for them to clear all selected items,开发者_开发技巧 probably will just be a SPAN tag or something similar.

Anyway ... using jQuery, how I would I clear the selection on the SELECT input element so no items are selected.


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:

$("#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 });


Only this worked for me:-

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


This solution worked for me...

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

This works on almost any input type.


i try something like

$("#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.

0

精彩评论

暂无评论...
验证码 换一张
取 消