我有一個電話號碼輸入和10個輸入字段,當你填寫每個號碼時,它跳到下一個。刪除鍵將焦點移動到以前的輸入字段
我希望它能夠返回到前一個字段,當我點擊刪除鍵(例如我輸入了一個不正確的數字在我的電話號碼)。我怎樣才能做到這一點?
看看我CodePen吧: http://codepen.io/Steve-Jones/pen/qdMjWb/
HTML
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<body>
<div class="phone-field">
(<input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5" autofocus="autofocus">
<input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
<input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">)
<input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
<input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
<input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5"> -
<input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
<input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
<input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
<input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
</body>
CSS
.phone-field {
margin: 50px;
font-size: 20px;
}
.phone-input {
width: 13px;
text-align: center;
font-size: 16px !important;
height: 1.75em;
border: 0;
outline: 0;
background: transparent;
border-bottom: 2px solid #ddd;
margin-right: 2px;
margin-left: 2px;
}
[placeholder]:focus::-webkit-input-placeholder {
transition: opacity 0.5s 0.0s ease;
opacity: 0;
}
jQuery的
$(document).ready(function(){
$('body').on('keyup', 'input.phone-input', function(){
if($(this).val().length === this.size){
var inputs = $('input.phone-input');
inputs.eq(inputs.index(this) + 1).focus();
}
});
});
代碼請!或給你的codepen – divy3993
@ divy3993更新完美的鏈接! –
好的,@Steve Jones看看我的答案能幫助你。 – divy3993