这是我的 jQuery 自动完成搜索框代码,在源代码中我正在推送 JSON 对象数组,例如[{displayName :'john',value:'12345',email:'john@gmail.com'},...]
var termTemplate = "<span class='ui-autocomplete-term'>%s</span>";
$('.searchboxx').autocomplete({
source: function(request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
response($.grep(contacts, function(contact) {
return matcher.test(contact.displayName)
|| matcher.test(contact.value) || matcher.test(contact.email);
}));
},
select: function(event, ui) {
var contact = ui.item;
}
}).data("ui-autocomplete")._renderItem = function(ul, contact) {
return $("<li>")
.append('<a><div class="row">' +
contact.displayName + " <" + contact.value + ">" + " [" + contact.rel + "]" +
'</div>' +
'</div></a>')
.appendTo(ul);
};
我的 ul 就像<li>john<12345>[mobile]</li>,.....
当我点击其中一个时,我在搜索框中得到 12345 但我希望名称出现在搜索框中,我该如何实现?我尝试在选择中添加代码和平
if(ui.item){
$('.searchboxx').val(contact.displayName);
}
但它没有任何建议?