我目前正在尝试在用户使用 Jquery 按下回车键时关注下一个输入类型。但是,由于某种原因,它没有检测到按键。
输入类型位于名为 mGrid 的 css 类中。
function addFocusSupport() {
$(".mGrid").find('input').each(function (index, element) {
// Here we get all the TextFields
alert($(this));
$(this).on("keypress",
function (e) {
if (e.KeyCode() === 13) {
// Focus next textfield
alert("Enter Pressed");
var nextElement = $('[index="' + (this.Index + 1) + '"]');
nextElement.focus();
alert(nextElement);
e.preventDefault();
}
// TODO: For last TextField, do nothing
});
});
}
所以我想做的是: 用户填写第一个输入,按下回车键,然后自动选择下一个文本框。将其视为按下一个选项卡。
但是 if (e.KeyCode() === 13) {
之后的事件永远不会触发?
最佳答案
就我而言,以下代码运行良好。
$('body').on('keydown', 'input, select', function(e) {
if (e.which === 13) {
var self = $(this), form = self.parents('form:eq(0)'), focusable, next;
focusable = form.find('input').filter(':visible');
next = focusable.eq(focusable.index(this)+1);
if (next.length) {
next.focus();
}
return false;
}
});
关于javascript - Jquery:在输入焦点下一个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46682428/