【发布时间】:2021-02-15 00:49:14
【问题描述】:
我正在尝试使用 Jquery 为地址自动完成构建一个自动完成输入字段。 我选择 geocodeapi 作为来源,它是免费的,而且我没有 google api 的信用卡。
这是文档https://geocodeapi.io/documentation#autocomplete 自动完成工作,请求和回答来了,但我不知道如何格式化或读取 json 以便它在自动完成中显示地址,我变得未定义
$(function() {
$("#customerAdress").autocomplete({
source: function(request, response) {
$.ajax({
url: "https://app.geocodeapi.io/api/v1/autocomplete?apikey=mykey",
dataType: "json",
data: {
text: request.term
},
success: function(data) {
console.log("here comes data for test")
console.log(data)
var data = $.map(data, function(obj) {
return {
label: obj.Street+','+obj.Housnumber+','+ obj.zip+','+ obj.city, //here is the problem i think
};
});
response(data);
}
});
},
minLength: 1,
});
});
最后,为什么可以使用层函数将结果还原到特定状态
【问题讨论】:
标签: javascript jquery json autocomplete