选择next()输入

dan*_*tch 18 jquery

我有以下html:

<div>
    <input type="text" maxlength="2" class="day-spin-month"></input>
    <span>/</span>
    <input type="text" maxlength="2" class="day-spin-day"></input>
    <span>/</span>
    <input type="text" maxlength="4" class="day-spin-year"></input>
</div>
Run Code Online (Sandbox Code Playgroud)

我试图将焦点转移到下一个输入.我有以下两种方式工作.

$('.day-spin-month').on('keyup', function () {
    if (this.value.length >= 2) {
       $(this).next().next().focus();
    }
});
$('.day-spin-day').on('keyup', function () {
    if (this.value.length >= 2) {
       $(this).next().next().focus();
    }
});
Run Code Online (Sandbox Code Playgroud)

小提琴:http://jsfiddle.net/dan_vitch/JbQaN/1/

$('.day-spin-month').on('keyup', function () {
    if (this.value.length >= 2) {
       var test=$(this).next()
       var test2=  $(test).next('input');
       test2.focus();
     }
});
$('.day-spin-day').on('keyup', function () {
    if (this.value.length >= 2) {
        var test=$(this).next()
        var test2=  $(test).next('input');
        test2.focus();
    }
});
Run Code Online (Sandbox Code Playgroud)

小提琴:http://jsfiddle.net/dan_vitch/JbQaN/2/

但它不会这样工作:

$('.day-spin-month').on('keyup', function () {
    if (this.value.length >= 2) {
       $(this).next('input').focus();
    }
});
$('.day-spin-day').on('keyup', function () {
    if (this.value.length >= 2) {
       $(this).next('input').focus();
    }
});
Run Code Online (Sandbox Code Playgroud)

小提琴:http://jsfiddle.net/dan_vitch/rJwyE/2/

我想使用不起作用的方式.我不确定它的选择器是不正确的,还是我明白的方式()

Pau*_*aul 59

.next()只查看DOM中的下一个兄弟元素.IE浏览器.

$('.day-spin-month').next('input');
Run Code Online (Sandbox Code Playgroud)

因为下一个兄弟是一个跨度而不是输入,所以不会匹配任何东西.

nextAll()查看所有下一个兄弟,所以你想要:

$(this).nextAll('input').first().focus();
Run Code Online (Sandbox Code Playgroud)