gpt4 book ai didi

javascript - 自动完成 : first item focused only when the user type on tab key

转载 作者:行者123 更新时间:2023-11-30 18:20:59 25 4
gpt4 key购买 nike

通过使用 jqueryUi autocomplete 我希望第一个项目仅在用户键入 tab 键 时自动聚焦。
我应该如何执行此任务?
使用指定的 autoFocus option true 初始化自动完成不符合我的目的!

有什么想法吗?


这是我的代码。
请参阅评论了解更多详情。

    element.autocomplete({
minLength: 3,
// the following option "autoFocus = true"
// make the first item focused when the user type the first 3 letters
// I would like the first item focused only when I type on TAB
autoFocus: false,
source: function (request, response) {
// some code
}
}).data('autocomplete')._renderItem = renderItem;

// the following piece of code works only when I type the first three letters,
// If I type four letters and then tab it does not work!
element.on('keyup', function (event) {
if (event.keyCode === 9) {
element.autocomplete( "option", "autoFocus", true );
}
});

最佳答案

是的,autoFocus 不会如您所愿。相反,您可以伪造用户在想要选择项目时通常必须执行的按键操作。

element.keydown(function(e){
if( e.keyCode != $.ui.keyCode.TAB ) return; // only pay attention to tabs

e.keyCode = $.ui.keyCode.DOWN; // fake a down error press
$(this).trigger(e);

e.keyCode = $.ui.keyCode.ENTER; // fake select the item
$(this).trigger(e);
});

演示:http://jsfiddle.net/uymYJ/8/

关于javascript - 自动完成 : first item focused only when the user type on tab key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12039219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com