Select2 and initSelection callback

后端 未结 2 1373
日久生厌
日久生厌 2020-12-28 14:03

On page load, I\'m trying to use initSelection to select ID 60 (specified value of the input field). I can\'t seem to get it to work properly.

The PHP scrip

相关标签:
2条回答
  • 2020-12-28 14:56

    Finally solved it! I figured select2 didn't want an array since it's a single value, so I selected the first element of the data.results array.

    callback(data.results[0]);
    

    And if you have set multiple:true, just accept the entire results array;

    callback(data.results);
    
    0 讨论(0)
  • You could also use this for less code:

        initSelection: function(element, callback) {
            return $.getJSON("/jQueryScripts/jQuerySelectListArtists.php?id=" + element.val(), null, function(data) {
                return callback(data[0]);
            });
        }
    

    I saw this example in the Select2 wiki, but i had to deal with callback(data) vs callback(data[0]) like you did.

    0 讨论(0)
提交回复
热议问题