jQuery select2 AJAX not working

∥☆過路亽.° 提交于 2020-01-03 07:49:22

问题


I'm using the jQuery select2 plugin and trying to get the AJAX to work with my ext data which is obviously not working and I'm just wondering if someone can point out what I'm doing wrong or missing something?

NOTE This is only for select v3.5.2

my js:

$('#cliselect').select2({
    ajax: {
        dataType: "json",
        url: "clientprojectpopulate.php",
        results: function (data) {
            return {results: data};
        }
    }
});

html:

<select id="cliselect" name="cliselect" style="width: 100%;" /></select>

my JSON returns (which I believe is valid):

[{"id":"62","text":"Alberta Innovates Health Solutions"},{"id":"4","text":"Alterna Savins & Credit Union"},{"id":"63","text":"BC Patient Safety & Quality Council"}]

回答1:


The Select2 control is updated to version 4.0. Now input fields are not working any longer and there should be select element.

The results have been changed to

processResults: function (data) {
    return {
      results: data
    };  
}

Inside the processResults function you can use them like this:

processResults: function (data) {
    var results = [];
    $.each(data, function (index, account) {
        results.push({
            id: account.AccountID,
            text: account.AccountName
        });
    });

    return {
        results: results
    };
}



回答2:


Figured out its because i was using <select>

It has to be an <input> for the ajax data to load...

<input type="hidden" id="cliselect" name="cliselect" style="width: 100%;" />


来源:https://stackoverflow.com/questions/22022607/jquery-select2-ajax-not-working

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