最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

uniapp中如何基于vue3實(shí)現(xiàn)輸入驗(yàn)證碼功能

 更新時(shí)間:2024年09月12日 10:29:01   作者:deku-yzh  
本文介紹了如何使用uniapp和vue3創(chuàng)建一個(gè)手機(jī)驗(yàn)證碼輸入框組件,通過(guò)封裝VerificationCodeInput.vue組件,并在父組件中引入,可以實(shí)現(xiàn)驗(yàn)證碼輸入功能,適合需要增加手機(jī)驗(yàn)證碼驗(yàn)證功能的開(kāi)發(fā)者參考使用

實(shí)現(xiàn)效果

描述

使用uniapp和vue3實(shí)現(xiàn)了手機(jī)獲取驗(yàn)證碼后,輸入驗(yàn)證碼的輸入框功能

具體實(shí)現(xiàn)代碼

下述代碼為實(shí)現(xiàn)驗(yàn)證碼輸入框封裝的組件VerificationCodeInput.vue

<template>
  <view class="container">
    <view class="input-container">
      <view v-for="index in 4" :key="index" class="verify-input">
        <input
          type="number"
          class="input-field"
          :ref="`input${index - 1}`"
          :maxlength="1"
          :focus="focusIndex === index - 1"
          @input="handleInput(index - 1, $event)"
          @focus="handleFocus(index - 1)"
        />
      </view>
    </view>
  </view>
</template>
<script lang="ts" setup>
import { ref, onMounted, nextTick } from 'vue'
// 焦點(diǎn)索引
const focusIndex = ref(0)
// 輸入值數(shù)組
const values = ref<string[]>(['', '', '', ''])
// 輸入框ref數(shù)組
const inputRefs = ref<(HTMLInputElement | null)[]>([])
/**
 * 處理每個(gè)輸入值函數(shù)
 * @param index - 序號(hào).
 * @param event - 輸入事件對(duì)象.
 */
const handleInput = (index: number, event: Event) => {
  // 獲取輸入框的值
  const input = event.target as HTMLInputElement
  const value = input.value
  if (value) {
    // 更新輸入值數(shù)組
    values.value[index] = value
    // 判斷是否還有下一個(gè)輸入框,如果有則聚焦
    if (index < 3) {
      nextTick(() => {
        focusIndex.value = index + 1
        const nextInput = inputRefs.value[index + 1]
        nextInput?.focus()
      })
    }
    // 判斷是否所有輸入框都已經(jīng)有值,如果有則觸發(fā)完成事件
    if (values.value.every((v) => v.length > 0)) {
      handleComplete()
    }
  } else {
    // 如果輸入值為空,則聚焦前一個(gè)輸入框
    if (index > 0) {
      focusIndex.value = index - 1
      nextTick(() => {
        const prevInput = inputRefs.value[index - 1]
        prevInput?.focus()
      })
    }
  }
}
// 處理焦點(diǎn)事件
const handleFocus = (index: number) => {
  focusIndex.value = index
}
// 處理完成事件
const handleComplete = () => {
  const code = values.value.join('')
  console.log('驗(yàn)證碼輸入完成:', code)
}
onMounted(() => {
  // 初始化焦點(diǎn)
  nextTick(() => {
    const firstInput = inputRefs.value[0]
    firstInput?.focus()
  })
})
</script>
<style>
.input-container {
  display: flex;
  justify-content: space-between;
}
.verify-input {
  width: 60px;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.input-field {
  width: 100%;
  height: 100%;
  text-align: center;
  font-size: 24px;
  border: none;
  border-bottom: 2px solid #ccc;
  outline: none;
}
</style>

最后在父組件中引入VerificationCodeInput.vue即可實(shí)現(xiàn)上圖效果

到此這篇關(guān)于uniapp中基于vue3實(shí)現(xiàn)輸入驗(yàn)證碼功能的文章就介紹到這了,更多相關(guān)vue3輸入驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

绍兴县| 阿瓦提县| 攀枝花市| 五寨县| 班戈县| 庆城县| 多伦县| 疏勒县| 大石桥市| 深泽县| 交城县| 南通市| 许昌市| 汉川市| 罗平县| 工布江达县| 衢州市| 大英县| 八宿县| 汪清县| 扎囊县| 新和县| 江源县| 丹凤县| 班戈县| 绥棱县| 宜宾县| 稻城县| 噶尔县| 柞水县| 罗山县| 镇雄县| 余江县| 北辰区| 房产| 宜黄县| 和平区| 镇巴县| 永善县| 邻水| 韶山市|