我的问题得到一个JSON(与页面标题+网址)的下拉列表仅显示项目,其中包括我搜索的单词(比如我搜索“google
” ,我的休息让我用,例如“google
”一个JSON和“g-mail
”,但下拉列表只显示“google
”)
当我alert()
比赛,变量i得到如“google
”和“g-mail
”(所以JSON是正确的!)
$(function(){
var obj = {};
var matches = [];
$(".typeahead").typeahead({
source: function (str, c) {
$.ajax({
url:'http://QUERYURL' + str +'EXAMPLE',
dataType: 'json',
cache: false,
success: function(data){
obj = {};
matches = [];
_.each(data.results, function(item){
matches.push(item.title);
obj[ item.title ] = "https:/"+item.url;
});
c(matches);
}
});
},
updater: function (selectedName) {
url = obj[ selectedName ];
window.open(url);
return selectedName;
}
});
});
确定这是有道理的...有一个简单的方法来显示JSON的所有结果?当我使用http://twitter.github.io/typeahead.js时,我得到了显示每个结果(但我得到不同的问题,所以我切换到bootstrap) – sibe94
我不确定你为什么要使用typeahead显示结果与用户输入的内容不符,这就是他们所做的。 – Starscream1984