【问题标题】:Delete Key Move Focus to Previous Input Field删除键将焦点移动到上一个输入字段
【发布时间】: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


【解决方案1】:

可以通过键码实现: 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();
        }
      }

    });
  });

查看您的代码,我发现了另一件事,在空格键光标上 到下一个索引。所以也涵盖这一点,我认为这是合适的。

键码

退格键 = 8

删除 = 46

空格键 = 32

【讨论】:

  • 你能完全用javascript吗,我的意思是不使用jquery?有需要!
【解决方案2】:

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

只需注意退格键并向后移动而不是向前移动。

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

【讨论】:

    【解决方案3】:

    只需检查关键事件是否删除,然后将焦点放在元素之前。我将您的 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();
          }
        });  
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多