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

后端 未结 13 2789
半阙折子戏
半阙折子戏 2020-12-09 07:52

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

相关标签:
13条回答
  • 2020-12-09 08:06

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

     $("#drp_assign_list option[value]").remove();
    
    0 讨论(0)
  • 2020-12-09 08:07

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

    0 讨论(0)
  • 2020-12-09 08:08

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-09 08:09

    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.

    0 讨论(0)
  • 2020-12-09 08:15

    In JQuery:

      $("#id option:selected").prop("selected", false);
      $("#id").multiselect('refresh');
    
    0 讨论(0)
  • 2020-12-09 08:17
    $('.selectpicker').selectpicker('deselectAll');
    

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

    0 讨论(0)
提交回复
热议问题