Select2 and initSelection callback

陌路散爱 提交于 2019-11-30 03:27:11
Mad Marvin

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);

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!