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