gpt4 book ai didi

javascript - 删除键 将焦点移动到上一个输入字段

转载 作者:搜寻专家 更新时间:2023-10-31 08:27:43 27 4
gpt4 key购买 nike

我有一个带有 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

$(document).ready(function(){

$('body').on('keyup', 'input.phone-input', function()
{
var key = event.keyCode || event.charCode;
var inputs = $('input.phone-input');
if(($(this).val().length === this.size) && key != 32)
{
inputs.eq(inputs.index(this) + 1).focus();
}
if( key == 8 || key == 46 )
{
var indexNum = inputs.index(this);
if(indexNum != 0)
{
inputs.eq(inputs.index(this) - 1).val('').focus();
}
}

});
});

Looking at your code i found one more thing, on space-bar cursor goes to next index. So also to cover that, i think this is suitable.

键码

退格键 = 8

删除 = 46

空格键 = 32

关于javascript - 删除键 将焦点移动到上一个输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31545741/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com