Html代碼,定義三個input輸入框申尤,文本長度最大為3
<input id="input1" type="text" onkeydown="check(this)" maxlength="3">
<input id="input2" type="text" onkeydown="check(this)" maxlength="3">
<input id="input3" type="text" onkeydown="check(this)" maxlength="3">
js代碼,處理onkeydown事件(當松開鍵盤按鍵的時候)
<script>
function check(obj) {
var val = parseInt(obj.id.split("avid")[1])
// 當input文本長度為3鸿脓,且不是最后一個input組件時犹菇,進入下一個input組件
if (document.getElementById(obj.id).value.length == 3 && val < 3) {
$('#avid'+ ++val).focus();
}
// 當input文本長度為0,且不是第一個input組件時闹蒜,進入上一個input組件
if (document.getElementById(obj.id).value.length == 0 && val > 1) {
$('#avid' + --val).focus();
}
}
</script>
[個人博客]四分體 - sifenti.com