0

我有以下内容:

<span class="xyz">Test</span><input type="text" class="hello">
<span class="xyz">Test</span><input type="text" class="hello">
<span class="xyz">Test</span><input type="text" class="hello">

我想自动关注下一个输入:

$(document).on('keyup', '.hello', function() {
    $(this).next('input').focus();
});

如果我删除代码,它工作正常。但是当代码在那里时,它不会。想法?

4

1 回答 1

3

.next('input')将不起作用,因为跟随的元素<input><span>.

尝试.nextAll()

$(document).on('keyup', '.hello', function() {
    if ($(this).nextAll('input').length)
        $(this).nextAll('input')[0].focus();
});

演示

于 2013-06-15T03:01:44.580 回答