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

前端 未结 10 1759
北海茫月
北海茫月 2020-12-28 13:55

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 pro

相关标签:
10条回答
  • 2020-12-28 14:28
    Maybe 
    
    $('#mySelect option').each(function(i, e)
    {
       e.selected = false
    });
    
    0 讨论(0)
  • 2020-12-28 14:29

    i try something like

    $("#my-Select").find('option').attr("selected","") ;
    
    0 讨论(0)
  • 2020-12-28 14:31

    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.

    0 讨论(0)
  • 2020-12-28 14:38

    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(); 
       });
     });
    
    0 讨论(0)
提交回复
热议问题