首页 > 解决方案 > 如何将焦点移动到Vuejs中的下一个输入字段?

问题描述

这是我的 codepen 链接https://codepen.io/santoshch/pen/LYxOoWO

我有 otp 字段,当所有 4 个字段时,如果我填写并按退格/删除按钮。然后只有它向后移动并删除输入的数字。

但是下面代码的问题是,当我在 otp 字段中输入单个数字并按退格/删除按钮时,它不会向后移动。(留在同一输入字段中)

// Shift focus to next input field if max length reached
        if (value.length >= maxlength) {          
          if (typeof this.activationKeyFields[index + 1] == 'undefined') {
            e.preventDefault();
            return;
          }
          this.$refs[`input-${parseInt(index + 1)}`][0].focus();
          e.preventDefault();
        }
      else{
          if (typeof this.activationKeyFields[index - 1] == 'undefined') {
          e.preventDefault();
          return;
        }
        this.$refs[`input-${parseInt(index - 1)}`][0].focus();
        e.preventDefault();
      }
      },

标签: javascriptvue.js

解决方案


推荐阅读