Javascript 关注下一个输入字段时,超出了jQuery最大调用堆栈大小

Javascript 关注下一个输入字段时,超出了jQuery最大调用堆栈大小,javascript,jquery,html,Javascript,Jquery,Html,我是新手 我有一张表格: <form name="code" action="*" method="post" autocomplete="off"> <input type="text" name="code" maxlength="1" autocomplete="off" class="a"> <input type="text" name="code" maxlength="1" autocomplete="off" class="b"> <in

我是新手

我有一张表格:

<form name="code" action="*" method="post" autocomplete="off">
<input type="text" name="code" maxlength="1" autocomplete="off" class="a">
<input type="text" name="code" maxlength="1" autocomplete="off" class="b">
<input type="text" name="code" maxlength="1" autocomplete="off" class="c">
<input type="text" name="code" maxlength="1" autocomplete="off" class="d">
<input type="text" name="code" maxlength="1" autocomplete="off" class="e">
<input type="text" name="code" maxlength="1" autocomplete="off" class="f last">
<button type="submit" class="full" value="" disabled="disabled"></button>
</form>
我想做的是,当填写字段
a
时。我想转到字段
b
等等

现在发生的情况是,如果触发了
条件,则抛出
超过最大调用堆栈大小的
错误

我做错了什么

还有更优雅的方法吗?

使用jQuery调用
.focus()
将导致相关事件处理程序立即运行,并且由于您是从“focusin”处理程序内部执行此操作,因此最终会出现无限递归。我总是通过在超时时间内执行
.focus()
操作来解决这个问题:

if (a.val().length == 1) {
    setTimeout(function() {
        a.next(b).focus();
    }, 1);
}
即使没有递归问题,在将焦点重定向到另一个元素之前,您也必须让当前的“焦点”工作完成。

您有“focusin”作为窗体的事件侦听器,但在事件处理程序中始终将焦点交给a或b,这导致了问题您的焦点()方法似乎会在每个循环中重新触发附加到侦听器的方法
if (a.val().length == 1) {
    setTimeout(function() {
        a.next(b).focus();
    }, 1);
}