我在我的搜索框中使用了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; } }); });
这是我的resut_object
"result_object":[{"productName":"samsung galaxy s3","manufacturer":"Samsung","productOptions":"Color;Memory","productOptiondesc":"Silver;32GB"},{"productName":"samsung salaxy s3","manufacturer":"Samsung","productOptions":"Color;Memory","productOptiondesc":"Graphite;32GB"},{"productName":"samsung galaxy s3","manufacturer":"Samsung","productOptions":"Color;Memory","productOptiondesc":"Silver;16GB"}]
您缺少结果数据的id属性。如果没有,则使选项“不可选择”。
例:
$('#e7').select2({ id: function(e) { return e.productName; }, });