Javascript 自动完成:仅当用户在tab键上键入时才聚焦第一项
通过使用Javascript 自动完成:仅当用户在tab键上键入时才聚焦第一项,javascript,jquery,jquery-ui,jquery-ui-autocomplete,Javascript,Jquery,Jquery Ui,Jquery Ui Autocomplete,通过使用jqueryUiautocomplete我希望只有当用户在选项卡键上键入时,第一项才会自动聚焦 我应该如何执行此任务? 使用指定的自动对焦选项初始化自动完成不符合我的目的 有什么想法吗 这是我的代码。 有关更多详细信息,请参阅评论 element.autocomplete({ minLength: 3, // the following option "autoFocus = true" // make the first ite
jqueryUi
autocomplete
我希望只有当用户在选项卡键上键入时,第一项才会自动聚焦
我应该如何执行此任务?
使用指定的自动对焦选项初始化自动完成不符合我的目的
有什么想法吗
这是我的代码。
有关更多详细信息,请参阅评论
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 );
}
});
是的,自动对焦不会做你想做的。相反,你可以假装用户在选择某个项目时通常必须按的按键
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);
});
演示:你有一些示例代码让我们看看吗?@JeffreyLo当然有。我用我的代码和一些评论更新了我的问题。谢谢你的时间。+1太好了。只有一个问题。单击选项卡时,我应该传递到另一个字段。这是。如何解决此问题?我建议找到下一个输入元素并对其调用.select()
。我更新了演示来实现这一点。