【发布时间】:2015-07-21 17:26:38
【问题描述】:
我有一个带有 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一个完美的链接
-
好的,@Steve Jones 看看我的回答是否对你有帮助。
标签: javascript jquery html css input