我正在尝试使用 typeahead.js 实现一个搜索框,用户可以在其中通过单击 typeahead 中的行项目或使用键盘上下选择条目并通过回车键导航到它们来导航到条目。
这是我的html
<div id="remote">
<input class="typeahead" type="text" placeholder="Search for people">
</div>
这是我的javascript
$(document).ready(function() {
var castDirectors = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: '../api/v1/search/people_typeahead',
remote: '../api/v1/search/people_typeahead?q=%QUERY',
dupDetector: function(remoteMatch, localMatch) {
return remoteMatch.value === localMatch.value;
}
});
castDirectors.initialize();
$('#remote .typeahead').typeahead(null, {
name: 'cast-directors',
displayKey: 'value',
source: castDirectors.ttAdapter(),
templates: {
empty: [
'<div class="empty-message">',
'no matching names',
'</div>'
].join('\n'),
suggestion: Handlebars.compile('<p><a id="typeahead" href="{{link}}">{{value}}</a></p>')
}
});
});
在输入类型=“文本”的情况下,如果我使用键盘导航并使用回车键选择,它只会填充文本框。我希望回车键等同于点击订单项。我需要更改什么?
最佳答案
设法将以下内容添加到我的 javascript 中并且运行良好
$('#remote .typeahead').on('typeahead:selected', function (e, datum) {
window.location.href = datum.link
});
关于javascript - typeahead.js 带有回车键的搜索框导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23556719/