JQuery Select2 - How to select all options

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

    Update 2019

    To programmatically select an option/item for a Select2 control, use the jQuery .val() method.

    You can also pass an array to val make multiple selections:

    $('#mySelect2').val(['1', '2']);
    $('#mySelect2').trigger('change');
    

    https://select2.org/programmatic-control/add-select-clear-items#selecting-options

提交回复
热议问题