使用Typeahead.js返回json数据

时间:2014-06-25 03:27:59

标签: javascript jquery json typeahead.js typeahead

我一直很高兴地使用Typeahead和Bootstrap 2一段时间。现在是升级的时候了。

使用Typeahead' s Prefetch method,我如何能够查找特定的json值并使用同一对象中的其他值作为输入值?

以下是我使用Twitter Bootstrap Typeahead插件实现目标的方法:

$('.restaurant_name input').typeahead({
  source: function(typeahead, query) {
    var _this = this;
    return $.ajax({
      url: "/restaurants.json?q=" + query,
      success: function(data) { 
        return typeahead.process(data);
      }
    });
  },            
  onselect: function (obj) {
    if (obj.City != ''){
      $(".restaurant_city input").attr("value", obj.City);      
    }
    if (obj.State != ''){
      $(".hotel_state input").attr("value", obj.State);     
    }
    if (obj.Zip != ''){
      $(".hotel_zip input").attr("value", obj.Zip);     
    }
  },
  property: "LocationName1"
});

使用Typeahead.js,我没有那么多偷看。有什么想法吗?

这是我的json文件中的两个条目片段," restaurants.json":

[{"LocationName1":"13 Monaghan","City":"New Orleans","State":"LA","Zip":"70116","ShortDescription":"Great deli sandwiches on historic Frenchmen St."},{"LocationName1":"7 on Fulton","City":"New Orleans","State":"LA","Zip":"70130","ShortDescription":"7 on Fulton, offers contemporary New Orleans cuisine with innovative dishes that are as eye-catching as they are delicious."}]

非常感谢任何帮助!

0 个答案:

没有答案