下面的代码工作正常。我正在寻求一个帮助我过滤json数据的帮助,即我想显示大于或等于3的对应id的标签。

在这种情况下,建议列表中应仅显示“印度”和“法国”。

请注意,我使用了Easy autocomplete plugin

如果有人可以帮助我,我将非常感谢。提前致谢

以下是我的json文件


  country.json


[
{
"id": "0.1",
"label": "America" },{
"id": "0.9",
"label": "UAE" },{
"id": "3.2",
"label": "India"},{
"id": "3.02",
"label": "France" } ]


HTML是


  index.php


<div class="form-group">
                    <input id="city" type="text" name="city" class="form-control" />
                </div>


和脚本是:


  script.js


$(function() { var options = { url: "../review/countries.json", getValue: function(element) {
 return element.label; list: {
match: {
  enabled: true
} }; $("#city").easyAutocomplete(options);

最佳答案

试试看



$(function() {

  var options = {
    url: "../review/countries.json",
    getValue: function(element) {
      var id = parseFloat(element.id);
      if (id > 3) {
        return element.label;
      } else {
        return '';
      }

    },
    list: {
      match: {
        enabled: true
      }

    }

  };
  $("#city").easyAutocomplete(options);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://easyautocomplete.com/dist/jquery.easy-autocomplete.min.js"></script>
<div class="form-group">
  <input id="city" type="text" name="city" class="form-control" />
</div>

10-08 02:08