Javascript 带有json数据的自动完成jquery ui无法正常工作小故障
我有以下javascript代码:Javascript 带有json数据的自动完成jquery ui无法正常工作小故障,javascript,jquery,json,jquery-ui,Javascript,Jquery,Json,Jquery Ui,我有以下javascript代码: $("#lcountry").autocomplete({ source: function (request, response) { $.ajax({ url: "https://graph.facebook.com/search?type=adcountry&limit=3", type: "GET", data: request,
$("#lcountry").autocomplete({
source: function (request, response) {
$.ajax({
url: "https://graph.facebook.com/search?type=adcountry&limit=3",
type: "GET",
data: request,
success: function (data) {
response($.map(data, function (el) {
return {
label: el.name,
value: el.country_code
};
}));
}
});
},
select: function (event, ui) {
// Prevent value from being put in the input:
this.value = ui.item.label;
// Set the next input's value to the "value" of the item.
$(this).next("input").value(ui.item.value);
event.preventDefault();
}
});
不幸的是,这不起作用。当我查看控制台时,这里是结果,显然我们可以看到国家,我希望能够在jquery autocomplete中返回名称值:
{
"data": [
{
"country_code": "US",
"name": "United States",
"supports_region": "true",
"supports_city": "true"
},
{
"country_code": "AR",
"name": "Argentina",
"supports_region": "false",
"supports_city": "true"
},
{
"country_code": "AU",
"name": "Australia",
"supports_region": "true",
"supports_city": "true"
}
],
"paging": {
"next": "https://graph.facebook.com/search?type=adcountry&limit=5&term=c&offset=5"
}
}
您需要使用
数据。数据
-数据
是一个对象,它包含一个键数据
,具有国家/地区列表
$("#lcountry").autocomplete({
source: function (request, response) {
$.ajax({
url: "https://graph.facebook.com/search?type=adcountry&limit=3",
type: "GET",
data: request,
success: function (data) {
response($.map(data.data, function (el) {
return {
label: el.name,
value: el.country_code
};
}));
}
});
},
select: function (event, ui) {
// Prevent value from being put in the input:
this.value = ui.item.label;
// Set the next input's value to the "value" of the item.
$(this).next("input").value(ui.item.value);
event.preventDefault();
}
});
演示: