jquery 键上下导航搜索结果
我有一个输入框,当您键入时会显示一个名为“建议”的 div 并使用搜索结果对其进行编译,每个结果都显示在 p 标签之间。
您可以使用鼠标选择结果,但我希望能够使用向上和向下键选择一个结果。
到目前为止,我已经知道,如果您按下建议框,则会再次出现(我知道它已经显示 - 但那是另一个问题),并尝试将焦点添加到第一个 p 标签。我怎样才能拥有它,以便当您继续按下它时,它会过滤结果,比如说每次添加和删除一类“活动”。显然上升也一样?
我发现了很多随机插件,但对于我想要实现的目标来说,它们似乎都很复杂且庞大。
$('#search input#q').keypress(function (e) {
switch (e.keyCode) {
// User pressed "up" arrow
case 38:
$('#suggestions').fadeOut('fast');
break;
case 40:
// User pressed "down" arrow
$('#search input#q').blur();
$('#suggestions').fadeIn();
$('#suggestions p').focus();
}
});
谢谢
I have an input box that as you type displays a div called "suggestions" and compiles it with search results, each result is displayed between p tags.
You can select a result using the mouse but I would like to be able to select one using up and down keys.
I have got it so far that if you press down the suggestions box appears again (I know it would already be displayed- but thats another issue), and attepts to add focus to the first p tag. how can I have it so as you keep pressing down it filters through the results lets say adds and removes a class of "active" each time. and obviously the same for going up as well?
I have found so many random plugins but they all seem to complex and bulky for what I am trying to achieve.
$('#search input#q').keypress(function (e) {
switch (e.keyCode) {
// User pressed "up" arrow
case 38:
$('#suggestions').fadeOut('fast');
break;
case 40:
// User pressed "down" arrow
$('#search input#q').blur();
$('#suggestions').fadeIn();
$('#suggestions p').focus();
}
});
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自动完成是 jQuery 团队支持的小部件。它能够使用箭头键进行选择并输入。它的核心也是一个简单的插件(能够轻松扩展功能)。
http://jqueryui.com/demos/autocomplete/
干杯,
阿维里克
Autocomplete is a jQuery team supported widget. It has the ability to select using arrows keys and enter. It is also a simple plugin at its core (with the ability to easily extend functionality).
http://jqueryui.com/demos/autocomplete/
Cheers,
awirick