【问题标题】:Select2 showing error for ajax requestSelect2 显示 ajax 请求错误
【发布时间】:2016-10-02 16:07:23
【问题描述】:

我想通过 ajax 请求将 select2 与数据一起使用。但是它显示了这个错误

Error: Option 'ajax' is not allowed for Select2 when attached to a <select> element.

HTML

<select id='myselect' class='select2-input select2'>
    <option></option>
    <option value="AL">Alabama</option>
    <option value="Am">Amalapuram</option>
    <option value="An">Anakapalli</option>
    <option value="Ak">Akkayapalem</option>
    <option value="WY">Wyoming</option>
</select>

JS

$(document).ready(function() {
var base_url = $('#baseurl').val();
$("#myselect").select2({
        placeholder: "Select a State",
        allowClear: true,
ajax: {
    url: base_url + 'selecttest',
    dataType: 'json',
    type: "GET",
    quietMillis: 50,
    data: function (term) {
        return {
            term: term.term
        };
    },
    results: function (data) {
        return {
            results: $.map(data, function (item) {
                return {
                    text: item.expense_detail,
                    id: item.user_id
                }
            })
        };
    }
}
}); 

像这样获取 JSON 响应

[{"expense_id":"2","user_id":"5","expense_detail":"abcdh1","amount":"123","expense_date":"2016-10-18","expense_type":"pocket","team_code":"0","team_id":"0"},{"expense_id":"3","user_id":"5","expense_detail":"hxyz1","amount":"123","expense_date":"2016-10-11","expense_type":"","team_code":"45664654","team_id":"46546"},{"expense_id":"4","user_id":"5","expense_detail":"abch2","amount":"123","expense_date":"2016-10-11","expense_type":"","team_code":"45664654","team_id":"46546"},{"expense_id":"5","user_id":"5","expense_detail":"abh3","amount":"123","expense_date":"2016-10-11","expense_type":"","team_code":"45664654","team_id":"46546"},{"expense_id":"6","user_id":"5","expense_detail":"ah4","amount":"123","expense_date":"2016-10-11","expense_type":"","team_code":"45664654","team_id":"46546"},{"expense_id":"7","user_id":"5","expense_detail":"h5","amount":"123","expense_date":"2016-10-11","expense_type":"","team_code":"45664654","team_id":"46546"}]

我正在使用这个 select2 版本

版权所有 2012 伊戈尔·韦恩伯格

版本:3.2 时间戳:2012 年 9 月 10 日星期一 10:38:04 PDT

非常感谢任何帮助..谢谢。

【问题讨论】:

  • 由于您使用的是低于版本4的Select2,因此您需要放置一个隐藏元素,如&lt;input type="hidden" id="hiddenElem" style="width:300px" /&gt;,并且您现有的初始化应为$("#hiddenElem").select2({...........否则适应Select2的版本4跨度>
  • @ArturFilipiak 问题是为什么显示该错误............错误:当附加到
  • @DavidR 谢谢我正在检查它。一旦它工作我会在这里评论。
  • 为什么被否决......!
  • @DavidR 我遇到了一个问题,我现在使用 4.0.3 ..现在每个请求为什么 'term' 查询字符串包含这个 ../selecttest?term%5B_type%5D=

标签: javascript php jquery ajax select2


【解决方案1】:

Select2 期望结果带有IDTEXT 属性,因此您需要将结果回调重写为,

            results: function (data) {
                var tmpResults = [];

                $.each(data, function (index, item) {
                    tmpResults.push({
                        'id': item.user_id,
                        'text': item.expense_detail,
                    });
                });
                return {
                    results: tmpResults
                };
             }

希望对你有帮助

【讨论】:

  • 你有类似return {term: term.term}; 的东西作为data 属性的输入,我猜你那里有一个额外的term,它必须是return {term: term};
  • 我输入了 term: term.term,因为 term 对象还包含一些其他属性,比如 _type,所以我只想将我们输入的字符串传递到搜索框中。所以 term.term 包含该字符串..它也返回 json 数据..我认为问题只是将 json 响应附加到该选择框............
  • 好的,这是一个例子,我把它放在一个模仿你的 json 的小提琴中(它托管在 myjson.com 中,作为休息调用使用)。它似乎按预期工作。请在此处查看 - jsfiddle.net/fyhsz9ra/747
  • 感谢@David R..它使用输入字段工作。但为什么它不能使用选择元素....正如我在我的 html
  • 基本上,Select2 库将给定的 html 输入控件作为 div 操作并填充/呈现 select2。
猜你喜欢
  • 1970-01-01
  • 2018-12-26
  • 1970-01-01
  • 2016-02-15
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 2014-01-17
  • 1970-01-01
相关资源
最近更新 更多