2016-08-13 114 views
0

我有一个json文件的url。在搜索输入字段中输入任何PIN码时,应显示PIN码建议。如何在输入字段中从json中搜索数据

这里是我的网址,

https://data.gov.in/api/datastore/resource.json?resource_id=6176ee09-3d56-4a3b-8115-21841576b2f6&api-key=

请给我建议的步骤或想法。

和,在此先感谢。

+0

这将是更好地保持 – gearsdigital

+0

您的API密钥的秘密...你正在寻找一个自动完成输入字段。很多方法来实现它。 Google是你的朋友。 – charsi

+0

没错,但它是免费的服务API。 –

回答

1

您可以使用autocomplete

$(function() { 
 
    $("#tags").autocomplete({ 
 
    source: function (request, response) { 
 
     $.ajax({ 
 
     url: "https://data.gov.in/api/datastore/resource.json?resource_id=6176ee09-3d56-4a3b-8115-21841576b2f6&api-key=<app key>", 
 
     dataType: "json", 
 
     success: function (data) { 
 
      response($.map(data.records, function (item) { 
 
      if (item.pincode.indexOf($("#tags").val()) == 0) 
 
       return { 
 
       label: item.pincode, // built the label like you want 
 
       value: item.pincode 
 
       }; 
 
      })); 
 
     } 
 
     }); 
 
    } 
 
    }); 
 
});
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/> 
 
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
 

 
<div class="ui-widget"> 
 
    <label for="tags">Iincode: </label> 
 
    <input id="tags"> 
 
</div>

+0

当我在输入字段中搜索时,我得到所有的搜索结果。我希望如果我搜索811214,我应该只获得811214建议,而不是所有的建议列表。 –

+0

@SandeepKumar代码段已更新。现在,当您获取数据时,将应用过滤器来限制建议 – gaetanoM

相关问题