我有一个服务,它返回在jQuery autocomplete中使用的以下JSON
[
{
"id":null,
"houseNumber":null,
"street":"2943 Iron Springs Pl",
"city":{
"id":null,
"city":"Castle Rock",
"countyId":null,
"stateId":null,
"properties":null
},
"state":{
"id":null,
"stateCode":"CO",
"stateName":null,
"counties":null,
"properties":null
},
"zipcode":{
"id":null,
"zipcode":"80109",
"properties":null,
"county":null
},
"propertyPhotos":null,
"pid":"pUuQ8Oce"
},
{
"id":null,
"houseNumber":null,
"street":"2943 Magowan Dr",
"city":{
"id":null,
"city":"Santa Rosa",
"countyId":null,
"stateId":null,
"properties":null
},
"state":{
"id":null,
"stateCode":"CA",
"stateName":null,
"counties":null,
"properties":null
},
"zipcode":{
"id":null,
"zipcode":"95405",
"properties":null,
"county":null
},
"propertyPhotos":null,
"pid":"ShDWSWcR"
}
]这是我的js代码。
<script type="text/javascript">
$(function() {
$("#autocomplete").autocomplete({
source : "/live-search",
minLength : 1
});
});
</script>当我尝试上面的代码时,我可以看到获得结果的文本框,但结果是空的。如何格式化结果,使其以以下格式显示地址?
street + ' ' + city.city + ', ' + state.stateCode + ' ' + zipcode.zipcode'
发布于 2019-05-18 09:46:37
尝试将您的source属性更改为:
source: function(request, response) {
$.ajax({
type: 'GET',
// "request" object has single property "term", which refers to the value currently in the text input.
url: "/live-search?term=" + request.term,
success: function(data) {
response($.map(data, function(item) {
return {
label: item.street + ' ' + item.city.city + ', ' + item.state.stateCode + ' ' + item.zipcode.zipcode,
value: item.id
}
}));
}
})
}https://stackoverflow.com/questions/56195051
复制相似问题