自动完成:仅当用户在Tab键上键入时,第一项才是焦点 [英] Autocomplete: first item focused only when the user type on tab key
本文介绍了自动完成:仅当用户在Tab键上键入时,第一项才是焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过使用jqueryUi
autocomplete
,我希望仅当用户在tab key
上键入时,第一项才自动聚焦.
我应该如何执行此任务?
用指定的autoFocus
option
true
初始化自动完成不适合我的目的!
By using jqueryUi
autocomplete
I would like the first item automatically focused only when the user type on tab key
.
How should I perform this task?
Initialising a autocomplete with the autoFocus
option
true
specified does not fit my purpose!
有什么想法吗?
这是我的代码.
请查看评论以获取更多详细信息.
Here is my code.
Please see the comment for more details.
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不会执行您想要的操作.取而代之的是,您可以伪造用户通常在选择项目时通常必须要做的按键.
Yeah, autoFocus won't do what you want. Instead you can fake the keypresses that a user would normally have to do when they want to select an item.
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/
这篇关于自动完成:仅当用户在Tab键上键入时,第一项才是焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文