我正在使用我的搜索框中的select2.我从我的网址获取结果,但我无法从中选择一个选项.我想使用’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; } }); });
这是我的resut_object
"result_object":[{"productName":"samsung galaxy s3","manufacturer":"Samsung","productOptions":"Color;Memory","productOptiondesc":"Silver;32GB"},{"productName":"samsung salaxy s3","productOptiondesc":"Graphite;32GB"},{"productName":"samsung galaxy s3","productOptiondesc":"Silver;16GB"}]
您缺少结果数据的id属性.
Select2 plugins wants an id field on data,如果没有,它使选项“不可选”.
原文链接:https://www.f2er.com/php/131092.html例:
$('#e7').select2({ id: function(e) { return e.productName; },});