jQuery UI自動完成插件會做你想做的。
看到這個演示http://jqueryui.com/demos/autocomplete/#remote-jsonp
的代碼演示:
$("#city").autocomplete({
source: function(request, response) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function(data) {
response($.map(data.geonames, function(item) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}))
}
})
},
minLength: 2,
select: function(event, ui) {
log(ui.item ? ("Selected: " + ui.item.label) : "Nothing selected, input was " + this.value);
},
open: function() {
$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
},
close: function() {
$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
}
});
可以使用download builder得到公正的自動完成的例子。作爲獎勵的自動完成是有主題的,你可以選擇一個themeroller準備好的主題或編輯一個並創建自己的。
謝謝,我今天就來看看jqueryui。我最初傳遞它是因爲該庫變得有點重要,並且我們一直在有意識地努力優化頁面加載時間 – RUtt 2010-05-26 14:40:35
只要您可以綁定到更改或發送事件,上面的「查詢跟蹤」想法仍然可以工作。 – 2010-05-26 14:47:31