如何在自动完成ajax选择函数上使用json数组?

时间:2015-04-12 20:18:13

标签: javascript jquery ajax json

使用Ajax自动完成jQuery UI我成功获得了搜索响应;但是,我需要通过将响应中的值传递给JavaScript来对选定结果执行操作。

这是我的JSON和我的代码段:

[{
  "id": 10,
  "value": "Amy",
  "country": "Algeria"
},
 {
   "id": 37,
   "value": "Amon",
   "country": "Belgium"
 }]

function Searchme() {
  var search_value = jQuery("#searchboxrecord").val();
  var search_url = "{{ path('search', {'value': 'text'}) }}";
  search_url = search_url.replace('text', search_value);
  $('#searchboxrecord').autocomplete({
    source: function (request, response) {
      $.ajax({
        type: "GET",
        url: search_url,
        data: {},
        dataType: "json",

        success: function (data) {
          response(data);

        }
      });
    },
    minLength: 1,
    select: function (event, ui) {
      $('.myText .show-content[data-type= "' + country + '"] ').fadeIn('slow');
    }
  });
}

1 个答案:

答案 0 :(得分:0)

我只是注意到我可以使用ui.item

访问响应json数组上的项目
    function Searchme(){
var search_value = jQuery ("#searchboxrecord").val();
var search_url = "{{ path('search', {'value': 'text'}) }}";
search_url = search_url.replace('text', search_value);
$('#searchboxrecord').autocomplete({
    source: function (request, response) {
        $.ajax({
            type: "GET",
            url: search_url,
            data: {},
            dataType: "json",

            success: function(data){
                response(data);
            }
        });
    },
    minLength: 1,
    select: function (event, ui) {
        var country = ui.item.country;

        $('.myText .show-content[data-type= "' + country + '"] ').fadeIn('slow');
        $('.myText .show-content[data-type!= "' + country + '"] ').fadeOut('slow');
    }
});
}