首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析jquery自动补全多维json数据

解析jquery自动补全多维json数据
EN

Stack Overflow用户
提问于 2019-05-18 08:36:45
回答 1查看 89关注 0票数 0

我有一个服务,它返回在jQuery autocomplete中使用的以下JSON

代码语言:javascript
复制
[  
   {  
      "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代码。

代码语言:javascript
复制
<script type="text/javascript">
    $(function() {
        $("#autocomplete").autocomplete({
            source : "/live-search",
            minLength : 1
        });
    });
</script>

当我尝试上面的代码时,我可以看到获得结果的文本框,但结果是空的。如何格式化结果,使其以以下格式显示地址?

street + ' ' + city.city + ', ' + state.stateCode + ' ' + zipcode.zipcode'

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-18 09:46:37

尝试将您的source属性更改为:

代码语言:javascript
复制
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
                }
            }));
        }
    })
}

更多信息- http://api.jqueryui.com/autocomplete/#option-source

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56195051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档