jQuery 选择 next() 输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8482241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:35:20  来源:igfitidea点击:

Selecting next() input

jquery

提问by dan_vitch

I have the following html:

我有以下 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>

I am attempting to change focus to the next input. I have it working the following 2 ways.

我正在尝试将焦点更改为下一个输入。我有以下两种方式工作。

$('.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();
    }
});

fiddle: http://jsfiddle.net/dan_vitch/JbQaN/1/

小提琴: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();
    }
});

fiddle: http://jsfiddle.net/dan_vitch/JbQaN/2/

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

but it doesnt work this way:

但它不是这样工作的:

$('.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();
    }
});

fiddle: http://jsfiddle.net/dan_vitch/rJwyE/2/

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

I would like to use the way that is not working. I am not sure if its my selector that incorrect, or the way I understand next()

我想使用不起作用的方式。我不确定是我的选择器不正确,还是我理解的方式 next()

回答by Paul

.next()only looks at the next sibling Element in the DOM. IE.

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

$('.day-spin-month').next('input');

Won't match anything since the next sibling is a span, not an input.

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

nextAll()looks at all the next siblings, so you want:

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

$(this).nextAll('input').first().focus();