我想從json中拉出一些數據並使用jquery自動完成顯示它們。jQuery自動完成自定義數據和顯示除了標籤,值
json數組包含ID,標題,日期。在顯示屏上,我想顯示標題和日期,並點擊我想解析該標題的特定ID。
所以目前我有:
$("input").autocomplete({
source: function (d, e) {
$.ajax({
type: 'GET',
url: url + mode + encodeURIComponent(d.term) + key,
dataType: "jsonp",
success: function (b) {
var c = [];
$.each(b.results, function (i, a, k) {
c.push(a.title + " " + a.date) // Displays Title and Date
});
e(c)
}
})
},
select: function (a, b) {
console.log(b);
// Appends an array with 2 keys: Value and Label.
// Both display the title and date as shown above.
}
}).data("autocomplete")._renderItem = function(ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>"+ item.label + "</a>")
// Here I want to append a.id as class of this
// but the only values are value and label.
.appendTo(ul);
};
所以,我怎麼能追加<li class="' + a.id + '">" + a.title + " " + a.date + "</li>"
對於標籤的自定義格式,改寫整個REQUEST_的_instead,你可以[自動完成原來的請求後,將其格式化(http://salman-w.googlecode.com/svn/trunk/jquery-ui -autocomplete /使用標籤 - 值pairs.html)。更多關於它[這裏](http://salman-w.blogspot.ro/2013/12/jquery-ui-autocomplete-examples.html)。 – machineaddict 2014-11-24 09:53:42