我正在使用this jquery ui自动完成,但我需要在每次击键时从json对象中获取数据,并在用户输入时将其放入下拉列表中。但该示例仅使用数组
<script type="text/javascript">
$(function() {
var availableTags = ["ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme"];
$("#request_artist").autocomplete({
source: availableTags
});
});
</script>
我在考虑在按键上执行ajax请求,但这与自动完成功能完全矛盾
答案 0 :(得分:3)
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");
}
});