javascript - 在 json 文件上使用嵌套数组自动完成

标签 javascript json autocomplete

这是我的json文件

"type": "FeatureCollection",
"name": "maps",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "IDLo": "1", "SoHieu": "1-1", "KyHieu": "C", "TenLo": "C1-1", "TenCty": "CMC Data Center"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 106.791800519464871, 10.854928689692501 ], [ 106.792069337729856, 10.855930098557222 ], [ 106.792653322236532, 10.855766231881775 ], [ 106.79231961680415, 10.854783029941672 ], [ 106.791800519464871, 10.854928689692501 ] ] ] } },
{ "type": "Feature", "properties": { "IDLo": "2", "SoHieu": "1-2", "KyHieu": "C", "TenLo": "C1-2", "TenCty": "ASCENDAS" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 106.79264868743887, 10.855779887550094 ], [ 106.792064702932166, 10.855943754285464 ], [ 106.791786615071828, 10.854942345054598 ], [ 106.79101723865827, 10.855151730898562 ], [ 106.790461062937595, 10.855306494254153 ], [ 106.789969774384346, 10.855424842648457 ], [ 106.789478485831097, 10.855688850436046 ], [ 106.78819928167357, 10.857819111634392 ], [ 106.790915273109462, 10.859412245764197 ], [ 106.791907119811313, 10.857746282442497 ], [ 106.792111050908886, 10.857518691103408 ], [ 106.792379869173871, 10.857291099590915 ], [ 106.792583800271444, 10.856999782201919 ], [ 106.792732113796944, 10.856544598212894 ], [ 106.792741383392297, 10.856116724630859 ], [ 106.79264868743887, 10.855779887550094 ] ] ] } }]

我想在 features.properties.TenCty 上自动完成搜索

这是javascript

jQuery(document).ready(function($) {
     $('#cty').autocomplete({
        source: function (request, response) {
        $.ajax({
            type: 'GET',
            url: "data/maps.json",
            dataType: 'json',
            data: request,
            success: function(data) {
                $.each(data.features, function(d){
                    response($.map(d.properties, function(item) {
                        console.log(item.TenCty);
                        return (item.TenCty);
                    }));
                })
            }
        }); 
   },  
   minLength: 2
  });
  });

但是我什么也得不到,控制台像这样 XHR finished loading: GET "http://localhost:81/blog/data/maps.json?term=fd"./p>

不知道哪里出错了。请帮忙。 非常感谢。

最佳答案

基本上,您必须将 success 回调更新为:

success: function(data) {
   var result = $.each(data.features, function(d) {
       return (d.properties.TenCty);
   })
   response(result)
}

但是您可以重新访问 JQuery UI Autocomplete Remove JsonP code samples.

关于javascript - 在 json 文件上使用嵌套数组自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51817334/

相关文章:

python - Python 变量的循环列表

javascript - 错误: Bad Twitter streaming request: 401 using Twit

android - 自动完成库 - 跨浏览器/移动兼容

javascript - 实现 jQuery 自动完成

eclipse - eclipse 中 C/C++ 的自动激活触发器

javascript - 数组包含通过引用调用的值,因此它们的指针应该变成相同的值并等于 true

javascript - 在 html 文件中包含 no cache 元标记是否也会阻止缓存该 html 文件中链接的 js 和 css 文件?

javascript - ajax 循环 get 完成后运行函数

javascript - 不在 div id 中的所有输入元素上的 JQuery .focusin 事件

Java Play框架解析JSON错误