jQuery UI的插件實際上允許您通過文本值和整數作爲返回類型發送。
這是我用於自動完成的通用代碼。
$("#c_where").autocomplete({
autoFill: true,
mustMatch: true,
selectFirst: true,
source: function(request, response) {
$.ajax({
type: "post",
url: "inc/ajax/json/hotels.php", // Upload this aswell
data: {
maxRows: 12,
term: request.term
},
success: function(data) {
response($.map(JSON.parse(data), function(item) {
return {
label: item.name,
value: item.name,
id: item.hid
}
}));
}
});
},
change: function(event, ui) {
/*if ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0) {
$(this).val('');
}*/
if (!ui.item) {
$(this).val('');
}
},
select: function(event, ui) {
$('#c_where').data("id", ui.item.id);
}
}).live('keydown', function (e) {
var keyCode = e.keyCode || e.which;
if((keyCode == 9 || keyCode == 13) && ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0)) {
$(this).val($(".ui-autocomplete li:visible:first").text());
}
});