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

Delete Key Move Focus to Previous Input Field

本文关键字:上一个 输入 字段 焦点 移动 删除      更新时间:2023-09-26

我有一个电话号码输入,有10个输入字段,当你填写每个数字时,它会跳到下一个。

我希望它能够回到上一个字段,当我点击删除键(例如,我在我的电话号码中输入了一个错误的数字)。我该怎么做呢?

查看我的CodePen:http://codepen.io/Steve-Jones/pen/qdMjWb/

<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();
        }
      }
    });
  });

看你的代码,我发现了一件事,在空格键光标去转到下一个索引。因此,我认为这是合适的。

键码

Backspace = 8

Delete = 46

空格键= 32

http://codepen.io/anon/pen/XbPgjJ

注意退格键,向后移动,而不是向前移动。

  if(e.keyCode == 8){
    var index = inputs.index(this);
    if (index != 0)
        inputs.eq(index - 1).val('').focus();    
  }

检查键事件是否被删除,然后将焦点放在元素之前。我更新你的js文件如下,它运行良好。

$(document).ready(function(){
    $('body').on('keyup', 'input.phone-input', function(e){
      if($(this).val().length === this.size){
        var inputs = $('input.phone-input');
        inputs.eq(inputs.index(this) + 1).focus();
      }
      if(e.keyCode == 8 || e.keyCode == 46){
        var inputs = $('input.phone-input');
        inputs.eq(inputs.index(this) - 1).focus();
      }
    });  
  });