使用包括 select2 在内的 Enter 键在元素中切换
Tab through elements using Enter key including select2
我正在使用以下代码使用回车键在表单元素之间切换。问题是这段代码跳过了 select2 个元素。
$('body').on('keydown', 'input, select', function(e) {
if (e.key === "Enter") {
var self = $(this), form = self.parents('form:eq(0)'), focusable, next;
focusable = form.find('input,a,select,button,textarea').filter(':not([disabled]):not([tabindex="-1"]):visible');
next = focusable.eq(focusable.index(this)+1);
if (next.length) {
next.focus();
} else {
//form.submit();
}
return false;
}
});
将您的 keydown
更改为 keyup
$('body').on('keyup', 'input, select', function(e)
原因是 keydown
已经在 select2
库中处理以选择项目
我正在使用以下代码使用回车键在表单元素之间切换。问题是这段代码跳过了 select2 个元素。
$('body').on('keydown', 'input, select', function(e) {
if (e.key === "Enter") {
var self = $(this), form = self.parents('form:eq(0)'), focusable, next;
focusable = form.find('input,a,select,button,textarea').filter(':not([disabled]):not([tabindex="-1"]):visible');
next = focusable.eq(focusable.index(this)+1);
if (next.length) {
next.focus();
} else {
//form.submit();
}
return false;
}
});
将您的 keydown
更改为 keyup
$('body').on('keyup', 'input, select', function(e)
原因是 keydown
已经在 select2
库中处理以选择项目