JQuery Select2 - How to select all options

后端 未结 18 739
死守一世寂寞
死守一世寂寞 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:25

    Info: The values for gSiteIDs are the values used when originally dynamically creating the select options. #selAllSites is the id of a checkbox and #siteID is the id of the select list you want to manipulate.

    var gSiteIDs = "8475, 9082, 2387, 1234";
    
    function selectAllSites() 
    {
        if($("#selAllSites").is(":Checked")) {
            $("#siteID").select2("val", [gSiteIDs]);                                
        } else {
            $("#siteID").select2("val", []);                                
        }
    }           
    

提交回复
热议问题