Php 无法从select2搜索结果中选择结果

Php 无法从select2搜索结果中选择结果,php,html,json,jquery-select2,Php,Html,Json,Jquery Select2,我正在使用搜索框上的select2。我正在从我的URL获取结果,但无法从中选择选项。我想使用“product.productName”作为选择后显示的文本。我有没有遗漏什么或者犯了什么错误。我已经包括了select2.css和select2.min.js、jquery.js function dataFormatResult(product) { var markup = "<table class='product-result'><tr>";

我正在使用搜索框上的select2。我正在从我的URL获取结果,但无法从中选择选项。我想使用“product.productName”作为选择后显示的文本。我有没有遗漏什么或者犯了什么错误。我已经包括了select2.css和select2.min.js、jquery.js

  function dataFormatResult(product) {
        var markup = "<table class='product-result'><tr>";

        markup += "<td class='product-info'><div class='product-title'>" +     product.productName + "</div>";
        if (product.manufacturer !== undefined) {
            markup += "<div class='product-synopsis'>" + product.manufacturer + "</div>";
        }
        else if (product.productOptions !== undefined) {
            markup += "<div class='product-synopsis'>" + product.productOptions + "</div>";
        }
        markup += "</td></tr></table>";
        return markup;
    }

    function dataFormatSelection(product) {
        return product.productName;
    }
    $(document).ready(function() {
        $("#e7").select2({
            placeholder: "Search for a product",
            minimumInputLength: 2,
            ajax: {
                url: myURL,
                dataType: 'json',
                data: function(term,page) {
                    return {
                        productname: term 
                    };
                },
                results: function(data,page) { 

                    return {results: data.result_object};
                }
            },
            formatResult: dataFormatResult, 
            formatSelection: dataFormatSelection, 
            dropdownCssClass: "bigdrop", 
            escapeMarkup: function(m) {
                return m;
            } 
        });
    });

您缺少结果数据的id属性。如果没有,则使选项“不可选择”

例如:

            $('#e7').select2({
                    id: function(e) { return e.productName; },
            });

我也遇到过同样的问题,解决这个问题的其他方法是:-

在您的响应对象中(在上面的响应产品详细信息对象中),必须有一个“id”作为该对象的键和值

示例:-上面给定的响应对象必须如下所示

{“id”:“1”,“productName”:“三星galaxy s3”,“制造商”:“三星”,“产品选项”:“颜色;内存”,“产品选项”):“银色;32GB”}

所以你不需要这个
id:function(object){return object.key;}

id参数可以是与对象属性名称相关的字符串,并且必须位于对象的根目录中。数据对象内部的文本

var fruits = [{code: 222, fruit: 'grape', color:'purple', price: 2.2},
  {code: 234,fruit: 'banana', color:'yellow', price: 1.9} ];

$(yourfield).select2(
 {
   id: 'code',
   data: { results: fruits, text: 'fruit' }
 }
);

因为我使用的是AJAX,所以对我有效的方法是在processResults上返回一些东西作为ID:

$(field).select2({
   ajax: {
        // [..] ajax params here
        processResults: function(data) {
            return {
                results: $.map(data, function(item) {
                    return {
                        // proccessResults NEEDS the attribute id here
                        id: item.code,
                        // [...] other attributes here
                        foo: item.bar,
                    }
                })
            }
        },
    },
});

节省了我的时间!!。。。他们有可怕的文件!。。他们的AJAX示例应该提到这一点……Telvin,链接断开了。。顺便说一句,添加ID对我来说没有什么不同。@AlexanderSuraphel这个答案发生在四年前,它应该可以解决OP问题,以防链接被破坏,所以我用示例代码指出它,所以引用并不重要。插件也得到了更新,很多东西都被改变了。现在我不知道你的问题是什么。对不起。@TelvinNguyen好的。对我来说,现在删除链接是有意义的。再加上没有人这么做是为了解决“OP”问题。人们在这里回答是为了帮助很多人。@AlexanderSuraphel我同意删除链接。它应该显示为什么需要id。我正在删除链接,因为它不再可用。但是,请不要将此作为个人信息,因为这对您没有帮助,请仔细阅读SO答案的工作原理(回答问题)
$(field).select2({
   ajax: {
        // [..] ajax params here
        processResults: function(data) {
            return {
                results: $.map(data, function(item) {
                    return {
                        // proccessResults NEEDS the attribute id here
                        id: item.code,
                        // [...] other attributes here
                        foo: item.bar,
                    }
                })
            }
        },
    },
});