写一个简单的例子,在http://jsfiddle.net/2RNFj/3/
你只需要提供的对象和它提供给Bloodhound
设置的typehead
来源:
var links = [{name: "abc", link: "http://www.example1.com"},
{name: "nbc", link: "http://www.example2.com"}];
var source = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: links
});
source.initialize();
$('#custom-templates .typeahead').typeahead(null, {
name: 'matched-links',
displayKey: 'name',
source: source.ttAdapter(),
templates: {
empty: [
'<div class="empty-message">',
'unable to find any Best Picture winners that match the current query',
'</div>'
].join('\n'),
suggestion: Handlebars.compile('<p><a href="{{link}}">{{name}}</a></p>')
}
});
谢谢!这可以工作,但有一个警告,只有点击链接才能工作,如果用户通过键盘导航到链接并单击输入,则不起作用。这怎么能被支持? –
我认为typeahead已经处理下拉项目的'Enter'按键事件。您可以添加事件处理程序来处理“Enter”事件以打开链接。 –
需要下载并添加Handlebars I recon。 – Wrench