帮助jQuery工作尝试在keyUp之后自动关注下一个输入字段,尽可能通用

时间:2010-11-01 14:05:40

标签: jquery next siblings

HTML

<input class="fr pinPass" type="password" name="pin1" id="pin1_1" tabindex="100"/>
<label>Enter the third number</label>

<br class="cb"/>

<input class="fr pinPass" type="password" name="pin1" id="pin1_2" tabindex="101"/>
<label>Enter the fourth number</label>                                      
<br class="cb"/>

<input class="fr pinPass" type="password" name="pin1" id="pin1_3" tabindex="102"/>  
<label>Enter the second number</label>

当前的jQuery

$(".pinPass").focus(function () {
    $(this).keyup(function(){
         $("input").siblings('.pinPass').filter(':first').focus();              
    });
});

问题:

我试图在每个输入字段中的keyUp之后的下一个输入字段上设置autoFocus。 到目前为止,我已经提出了一直保持循环直到它被选中的最后一个解决方案,或者如果我使用:first filter,它总是返回到它在页面上找到的第一个。

我认为我不能使用next(),因为它不是集合中的下一个节点。 每个输入字段总会有标签分隔。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

您可以使用:first,但.next()只会返回非常的下一个 相反,您希望.nextAll()使用选择器,如下所示:

$(".pinPass").keyup(function(){
  $(this).nextAll('.pinPass:first').focus();
});

You can test it out here

答案 1 :(得分:0)

接下来可以有选择器。

$(this).next('input');