javascript jQuery select2 AJAX 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22022607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:13:58  来源:igfitidea点击:

jQuery select2 AJAX not working

javascriptjqueryajaxjquery-select2

提问by rubberchicken

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?

我正在使用 jQuery select2 插件并试图让 AJAX 处理我的 ext 数据,这显然不起作用,我只是想知道是否有人可以指出我做错了什么或遗漏了什么?

NOTEThis is only for select v3.5.2

注意这仅适用于选择 v3.5.2

my js:

我的js:

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

html:

html:

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

my JSON returns (which I believe is valid):

我的 JSON 返回(我认为这是有效的):

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

采纳答案by rubberchicken

Figured out its because i was using <select>

弄清楚它是因为我正在使用 <select>

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

它必须是<input>ajax 数据加载...

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

回答by Sajjad Ali Khan

The Select2control is updated to version 4.0. Now input fields are not working any longer and there should be selectelement.

选择二控制更新到4.0版本。现在输入字段不再工作,应该有select元素。

The results have been changed to

结果已更改为

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

Inside the processResultsfunction you can use them like this:

processResults函数内部,您可以像这样使用它们:

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

    return {
        results: results
    };
}