JQuery Select2 - How to select all options

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

    using Select 2 DEMO

    $("#e1").select2();
    $("#checkbox").click(function(){
        if($("#checkbox").is(':checked') ){
            $("#e1 > option").prop("selected","selected");// Select All Options
            $("#e1").trigger("change");// Trigger change to select 2
        }else{
            $("#e1 > option").removeAttr("selected");
            $("#e1").trigger("change");// Trigger change to select 2
         }
    });
    
    $("#button").click(function(){
           alert($("#e1").val());
    });
    
    Select All
    
    
    

    You need code As shown in DEMO2 for Simple Select

    $("#checkbox").click(function(){
        if($("#checkbox").is(':checked') ){
            $("select > option").prop("selected","selected");
        }else{
            $("select > option").removeAttr("selected");
         }
    });
    
    $("#button").click(function(){
           alert($("select").val());
    });
    
     Select All
    
    
    

提交回复
热议问题