How to set selected value of jquery select2?

后端 未结 28 1444
野趣味
野趣味 2020-11-30 00:00

This belong to codes prior to select2 version 4

I have a simple code of select2 that get data from ajax



        
28条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 00:41

    Also as I tried, when use ajax in select2, the programmatic control methods for set new values in select2 does not work for me! Now I write these code for resolve the problem:

    $('#sel')
        .empty() //empty select
        .append($("

    You can test complete sample code in here link: https://jsfiddle.net/NabiKAZ/2g1qq26v/32/
    In this sample code there is a ajax select2 and you can set new value with a button.

    $("#btn").click(function() {
      $('#sel')
        .empty() //empty select
        .append($("
    
    
    
    
    
    
    
    
    
    

提交回复
热议问题