場景:PC端

樣式:
<div class="verification-code-input"><input v-model="code[index]" v-for="(_, index) in 5" :key="index" type="text" maxlength="1" @input="handleInput(index)" :ref="'inputRefs' + index" class="input-item"></div>
函數:
數據: code: ['', '', '', '', '', ''], handleInput(index) {// 限制每個input只能輸入一個字符// 這里還可以添加其他邏輯,比如驗證碼格式驗證const value = this.code[index];if (value.length === 1 && index < 4) {this.$nextTick(() => {this.$refs[`inputRefs${Number(index) + 1}`][0].focus();});}console.log(this.code);if (index == 4) { 這個場景的驗證碼是5位數console.log('輸入完畢');}},