2011-12-12 36 views
15

Tôi có html sau:Lựa chọn tiếp theo() đầu vào

<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> 

Tôi đang cố gắng để thay đổi tập trung vào đầu vào tiếp theo. Tôi có nó làm việc theo 2 cách sau.

$('.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/

$('.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/

nhưng nó không làm việc theo cách này:

$('.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/

Tôi muốn sử dụng cách không hoạt động. Tôi không chắc liệu bộ chọn của tôi có đúng hay không, hoặc cách tôi hiểu tiếp theo()

+0

http://stackoverflow.com/questions/290535/best-way-to-find-next-form-input-element- in-jquery –

Trả lời

46

.next() chỉ xem xét phần tử anh chị em tiếp theo trong DOM. I E.

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

Sẽ không phù hợp với bất cứ điều gì kể từ khi anh chị em tiếp theo là một khoảng, không phải là một đầu vào.

nextAll() nhìn vào tất cả các anh chị em tiếp theo, vì vậy bạn muốn:

$(this).nextAll('input').first().focus(); 
Các vấn đề liên quan