How to set selected value of jquery select2?

后端 未结 28 1134
野趣味
野趣味 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条回答
  • 2020-11-30 00:49

    Sometimes, select2() will be loading firstly, and that makes the control not to show previously selected value correctly. Putting a delay for some seconds can resolve this problem.

    setTimeout(function(){                  
        $('#costcentreid').select2();               
    },3000);
    
    0 讨论(0)
  • 2020-11-30 00:49
    $('#inputID').val("100").select2();
    

    It would be more appropriate to apply select2 after choosing one of the current select.

    0 讨论(0)
  • 2020-11-30 00:52

    Just to add to anyone else who may have come up with the same issue with me.

    I was trying to set the selected option of my dynamically loaded options (from AJAX) and was trying to set one of the options as selected depending on some logic.

    My issue came because I wasn't trying to set the selected option based on the ID which needs to match the value, not the value matching the name!

    0 讨论(0)
  • 2020-11-30 00:53

    Html:

    <select id="lang" >
       <option value="php">php</option>
       <option value="asp">asp</option>
       <option value="java">java</option>
    </select>
    

    JavaScript:

    $("#lang").select2().select2('val','asp');
    

    jsfiddle

    0 讨论(0)
  • 2020-11-30 00:53

    An Phan's answer worked for me:

    $('#inputID').select2('data', {id: 100, a_key: 'Lorem Ipsum'});
    

    But adding the change trigger the event

    $('#inputID').select2('data', {id: 100, a_key: 'Lorem Ipsum'}).change();
    
    0 讨论(0)
  • 2020-11-30 00:55

    This may help someone loading select2 data from AJAX while loading data for editing (applicable for single or multi-select):

    During my form/model load :

      $.ajax({
            type: "POST",
            ...        
            success: function (data) {
              selectCountries(fixedEncodeURI(data.countries));
             }
    

    Call to select data for Select2:

    var countrySelect = $('.select_country');
    function selectCountries(countries)
        {
            if (countries) {
                $.ajax({
                    type: 'GET',
                    url: "/regions/getCountries/",
                    data: $.param({ 'idsSelected': countries }, true),
    
                }).then(function (data) {
                    // create the option and append to Select2                     
                    $.each(data, function (index, value) {
                        var option = new Option(value.text, value.id, true, true);
                        countrySelect.append(option).trigger('change');
                        console.log(option);
                    });
                    // manually trigger the `select2:select` event
                    countrySelect.trigger({
                        type: 'select2:select',
                        params: {
                            data: data
                        }
                    });
                });
            }
        }
    

    and if you may be having issues with encoding you may change as your requirement:

    function fixedEncodeURI(str) {
            return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%22/g,"");
    
        }
    
    0 讨论(0)
提交回复
热议问题