vue 实现 手机号中间4位分格输入框(暂无选中标识

vue 实现 手机号中间4位分格输入框

效果图

bash 复制代码
<!--4位分格输入框-->
<!--<template>
  <div>
    <div style="display: flex;">
      <div class="phone-input">
        <input
          v-for="(digit, index) in digits"
          :key="index"
          type="tel"
          :ref="`input-${index}`"
          v-model="digits[index]"
          maxlength="1"
          @input="handleInput(index)"
          @keydown="handleKeyDown(index, $event)"
          @focus="handleFocus(index)"
          class="digit"
          :class="{ 'active': activeIndex === index }"
        >
      </div>
      <div class="getCode" @click="validatePhoneNumber">获取验证码</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      digits: ['', '', '', '', '', '', '', '', '', '', ''],
      activeIndex: ''
    }
  },
  methods: {
    /*handleInput(index) {
      if (this.digits[index] && index < this.digits.length - 1) {
        this.focusNextInput(index);
      }
    },*/
    handleInput(index) {
      // Don't allow input to jump to next if the current index is 3 or 4
      if ((index === 3 || index === 4) && this.digits[index]) {
        return;
      }

      // Handle input logic for other cases
      if (this.digits[index] && index < this.digits.length - 1) {
        this.focusNextInput(index);
      }
    },
    handleKeyDown(index, event) {
      if (event.key === 'Backspace' && index > 0 && !this.digits[index]) {
        event.preventDefault();
        this.focusPreviousInput(index);
      }
      if (index===0&&event.key === 'Backspace') {
        this.activeIndex = '';
      }
    },
    handleFocus(index) {
      this.activeIndex = index;
    },
    /*focusNextInput(index) {
      this.$refs[`input-${index + 1}`][0].focus();
    },*/
    focusNextInput(index) {
      if (index === 2) {
        this.$refs[`input-${index + 2}`][0].focus();
      } else {
        this.$refs[`input-${index + 1}`][0].focus();
      }
    },
    focusPreviousInput(index) {
      this.$refs[`input-${index - 1}`][0].focus();
    },
    validatePhoneNumber() {
      const phoneNumber = this.digits.join('');
      // Perform phone number validation logic here
      // 针对手机号进行校验逻辑的代码
      this.activeIndex = '';
    }
  },
  mounted() {
    this.$nextTick(() => {
      // Focus on the first input when the component is mounted
      // this.$refs[`input-0`][0].focus();

      this.$nextTick(() => {
        const phone = '13240865213'; // Replace with actual phone number
        for (let i = 0; i < this.digits.length; i++) {
          if (i < 3 || i > 6) {
            this.digits[i] = phone[i];
          }
        }
        this.$refs[`input-3`][0].focus();
      });
    });
  },
  computed: {
    inputs() {
      return this.$refs.inputs;
    }
  }
}
</script>

<style>
.phone-input {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 40px;
  width: 615px;
}

.phone-input input {
  width: calc(100% / 11);
  height: 100%;
  border: solid 1px #ededed;
  margin-right: 15px;
  outline: none;
  text-align: center;
  font-size: 16px;
  border-radius: 6px;
  color: #0075ff;
}

.phone-input input.active {
  border-color: #0075ff;
}

.getCode {
  width: 120px;
  height: 40px;
  line-height: 40px;
  border: solid 1px #ededed;
  border-radius: 20px;
  color: #666;
  font-size: 16px;
  text-align: center;
}

.getCode:hover {
  cursor: pointer;
}
</style>-->          <el-table-column type="selection" width="55" align="center"></el-table-column>
相关推荐
y先森1 小时前
CSS3中的弹性布局之侧轴的对齐方式
前端·css·css3
new出一个对象4 小时前
uniapp接入BMapGL百度地图
javascript·百度·uni-app
你挚爱的强哥5 小时前
✅✅✅【Vue.js】sd.js基于jQuery Ajax最新原生完整版for凯哥API版本
javascript·vue.js·jquery
y先森6 小时前
CSS3中的伸缩盒模型(弹性盒子、弹性布局)之伸缩容器、伸缩项目、主轴方向、主轴换行方式、复合属性flex-flow
前端·css·css3
前端Hardy6 小时前
纯HTML&CSS实现3D旋转地球
前端·javascript·css·3d·html
susu10830189116 小时前
vue3中父div设置display flex,2个子div重叠
前端·javascript·vue.js
IT女孩儿7 小时前
CSS查缺补漏(补充上一条)
前端·css
吃杠碰小鸡8 小时前
commitlint校验git提交信息
前端
天天进步20158 小时前
Vue+Springboot用Websocket实现协同编辑
vue.js·spring boot·websocket
虾球xz9 小时前
游戏引擎学习第20天
前端·学习·游戏引擎