1计维、創(chuàng)建一個(gè)form表單
<form name=card action="" method=post>
<input onkeydown="T1_onkeyup()" maxLength=1 name=T1>
<input onkeydown="T2_onkeyup()" maxLength=1 name=T2>
<input onkeydown="T3_onkeyup()" maxLength=1 name=T3>
<input onkeydown="T4_onkeyup()" maxLength=1 name=T4>
</form>
onkeydown:鍵盤按下事件
maxLength:input最大輸入位數(shù)
可以添加type屬性用來限制只輸入數(shù)字,屬性值為tel
2秧骑、引入jquery文件
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
3渣淤、編寫js代碼
<script>
function T1_onkeyup() {
if(document.card.T1.value.length==1){
document.card.T2.focus();
}
}
function T2_onkeyup() {
var oEvent = window.event;
if (oEvent.keyCode == 8 && document.card.T2.value.length==0) {
document.card.T1.focus();
}
if(document.card.T2.value.length==1){
document.card.T3.focus();
}
}
function T3_onkeyup() {
var oEvent = window.event;
if (oEvent.keyCode == 8 && document.card.T3.value.length==0) {
document.card.T2.focus();
}
if(document.card.T3.value.length==1){
document.card.T4.focus();
}
}
function T4_onkeyup() {
var oEvent = window.event;
if (oEvent.keyCode == 8 && document.card.T4.value.length==0) {
document.card.T3.focus();
}
}
</script>
oEvent.keyCode == 8 :獲取按下的鍵值為8(刪除鍵)時(shí)回退上一個(gè)input框