支付密码方框样式 vue3语法(开箱即用)

直接上代码

  <div class="verification-container">
         <input v-for="(code, index) in verificationCodes" :key="index"
          class="verification-input"
          type="password"
          v-model="verificationCodes[index]"
          @input="handleInput(index, $event)"
          @keydown="handleKeyDown(index, $event)" 
          maxlength="1" />
  </div>
<script>
let verificationCodes=ref(['', '', '', '', '', '']);  //输入的密码
// 输入框失去焦点时触发
const handleInput = (index, event) => {
  let zc = /^\d+$/  // 来个正则效验一波
  const value = event.target.value;  // 每次输入的值
  //数字类型效验
  if (!zc.test(value)) {
    return verificationCodes.value[index] = ''
  }
  //效验成功后 把值放入到 数组里对应的下标内
  verificationCodes.value[index] = value;  
  // 自动跳到下一个输入框
  if (value && index < verificationCodes.value.length - 1) {
     // 使用nextElementSibling跳转到下一个InPut框(相当于tab键进入下一个输入框)
    const nextInput = event.target.nextElementSibling;
    if (nextInput) {
      nextTick(() => {
        nextInput.focus();
      });
    }
  }
};
// 处理删除
const handleKeyDown = (index, event) => {
    //   event.key === 'Backspace' 相当于退格键        
  if (event.key === 'Backspace' && !event.target.value && index > 0) {
    const prevInput = event.target.previousElementSibling;
    if (prevInput) {
      nextTick(() => {
        prevInput.focus();
      });
    }
  }
};
// 确定密码
const changqd = async () => {
    let paymentParams = verificationCodes.value.join('')
    if (paymentParams.length < 6) {
        return message.error(`请输入完整密码`);
    }
    console.log(paymentParams,'这个就是拿到的密码值了 列如:123456 这种 ')
    //下面就是调用自己的接口了
    
}
<script>
.verification-container {
    display: flex;
}
.verification-input {
    width: 40px;
    height: 40px;
    margin-right: 10px;
    text-align: center;
    font-size: 18px;
    border: 1px solid #ccc;
    border-radius: 5px;
}
.verification-input:focus {
    outline: none;
    border-color: #007bff;
    box-shadow: 0 0 5px #007bff;
}

感觉好用小伙伴可以 点赞 收藏 加关注 会持续添加更多实用开箱即用功能

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值