[英]Display data in autocomplete jQuery UI
I created an autocomplete function, when I console.log it displays me all my files that is in remote JSON file, but When I try to display it when I start Type on input it doesn't show nothing, no errors but it doesn't work at all. 我创建了一个自动完成功能,当我console.log时,它向我显示远程JSON文件中的所有文件,但是当我尝试在输入中启动Type时显示它时,它什么也不显示,没有错误,但是它没有显示。根本不工作。 Also I want to make this multi select autocomplete.
我也想使这个多选自动完成。 But right now I want it only display suggestions when I start type on input.
但是现在我只希望在开始输入时才显示建议。
$(function () {
$("#city").autocomplete({
source: function (request, response) {
$.ajax({
url: $('#city').attr('data-source'),
success: function (data) {
for (var i = 0; i < data.length; i++) {
data[i].loc_name
}
}
})
}
})
})
JSON JSON
[{"population":1729119,"token":"167|7|179|1296|55544|0","loc_name":"Warszawa"},{"population":758463,"token":"167|6|135|976|7644|0","loc_name":"Krak\u00f3w"},{"population":718960,"token":"167|5|113|789|58247|25218","loc_name":"\u0141\u00f3d\u017a Teofil\u00f3w"},{"population":718960,"token":"167|5|113|789|58247|25340","loc_name":"\u0141\u00f3d\u017a G\u00f3rna"},{"population":718960,"token":"167|5|113|789|58247|25282","loc_name":"\u0141\u00f3d\u017a \u0141askowice"}]
The issue is because your AJAX logic isn't quite right. 问题是因为您的AJAX逻辑不太正确。 Once the AJAX completes you need to provide the received data to the
response
callback, like this: AJAX完成后,您需要将接收到的数据提供给
response
回调,如下所示:
$("#city").autocomplete({
source: function (request, response) {
$.ajax({
url: $('#city').data('source'),
success: function(data) {
var output = data.map(function(o) {
return {
label: o.loc_name,
value: o.token
}
});
response(output);
}
})
}
})
This is assuming that the format that data
is returned in matches the format that autocomplete expects. 假定返回
data
的格式与自动完成所期望的格式匹配。 If not you'll have to modify the array - preferably on the server. 如果不是,则必须修改阵列-最好在服务器上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.