JQuery Select2 - How to select all options

后端 未结 18 781
死守一世寂寞
死守一世寂寞 2020-12-02 10:50

I\'m using jQuery select2 multi select dropdown. I need to select all options in a dropdown from code. Basically there is a Select All checkbox on which this functionality h

18条回答
  •  不知归路
    2020-12-02 11:27

    $(document).ready(function() {
        $("#my-select").select2();
    });
    
    function selectAll() {
        $("#my-select > option").prop("selected", true);
        $("#my-select").trigger("change");
    }
    
    function deselectAll() {
        $("#my-select > option").prop("selected", false);
        $("#my-select").trigger("change");
    }
    .button-container {
      margin-bottom: 10px;
    }
    
    #my-select {
      width: 200px;
    }
    
    
    
    
    

    This is the simplest way

    function selectAll() {
      $("#my-select > option").prop("selected", true);
      $("#my-select").trigger("change"); 
    }
    

提交回复
热议问题