在用户输入并调用typeahead:render后,我能够使用以下代码获得所有Twitter Typeahead建议。我想一直隐藏下拉列表,只在数组中获取建议。有没有办法做到这一点,因为 typeahead:render 可能需要打开下拉列表。
var bloodhoundData = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: localData
});
$('filter .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
source: bloodhoundData,
limit: 99999
}).on('typeahead:render', getSuggestions);
function getSuggestions() {
var suggestions = Array.prototype.slice.call(arguments, 1);
}
由于Bloodhound.js是一个独立的库,因此您不必使用typeahead。 您可以将寻血猎犬的输入绑定到普通文本输入,并检查 get 方法的结果。
像这样的东西可能会起作用,q
是输入中的文本(借用自 NFL 团队示例):
var myBloodhound = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
identify: function(obj) { return obj.name; },
local: localData
});
function getMyData(q, sync) {
myBloodhound.search(q, sync);
}
您可以在此处查看寻血猎犬文档和此处的示例。