Clear dropdown using jQuery Select2

前端 未结 15 1931
情深已故
情深已故 2020-11-28 11:35

I\'m trying to programmatically clear a drop down using the fantastic Select2 library. The drop down is dynamically filled with a remote ajax call using the Select2 qu

相关标签:
15条回答
  • 2020-11-28 12:11

    Method proposed @Lelio Faieta is working for me, but because i use bootstrap theme, it remove all bootstrap settings for select2. So I used following code:

    $("#remote option").remove();
    
    0 讨论(0)
  • 2020-11-28 12:12

    I'm a little late, but this is what's working in the last version (4.0.3):

    $('#select_id').val('').trigger('change');
    
    0 讨论(0)
  • 2020-11-28 12:12

    I found the answer (compliments to user780178) I was looking for in this other question:

    Reset select2 value and show placeholdler

    $("#customers_select").select2("val", "");
    
    0 讨论(0)
  • 2020-11-28 12:14

    You can use this or refer further this https://select2.org/programmatic-control/add-select-clear-items

    $('#mySelect2').val(null).trigger('change');
    
    0 讨论(0)
  • 2020-11-28 12:15

    You should use this one :

       $('#remote').val(null).trigger("change");
    
    0 讨论(0)
  • 2020-11-28 12:16

    This is the proper one, select2 will clear the selected value and show the placeholder back .

    $remote.select2('data', null)
    
    0 讨论(0)
提交回复
热议问题