在輸入字符數maxlength
後,我需要關注下一個輸入。問題是,下一個輸入並不總是一個兄弟:找到下一個與jquery時沒有兄弟
jQuery("input").on('input', function() {
if (jQuery(this).val().length == jQuery(this).attr('maxlength')) {
jQuery(this).next("input").focus();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
When you have write 4 char, the focus have to switch to the next input:
<div>
OK
<input maxlength="4">
<input maxlength="4">
</div>
KO
<input id="inputWithTheIssue">
也許一個解釋可以幫助OP和未來的讀者 – Adjit
當'currIndex + 1'沒有返回一個jQuery對象,然後嘗試調用'focus()'時,這會拋出一個錯誤嗎? –
不,它不會。如果您試圖訪問它的屬性或屬性值,它只會拋出未定義的錯誤。事件是好的。 – DinoMyte