使用Vant組件做驗(yàn)證碼填寫功能,自動跳到下一個輸入框

image.png

效果如圖所示,填寫的驗(yàn)證碼自動跳到下一個,監(jiān)聽填寫完之后自動校驗(yàn)驗(yàn)證碼正確性。使用了Vant組件的van-password-inputvan-number-keyboard,沒有使用Vant自帶的模態(tài)框組件,代碼如下:

1.template
    <!-- 填寫邀請碼 -->
    <div class="mask-box" v-show="showMask">
      <div class="context-box">
        <div class="close-img">
          <van-icon name="cross" size="20px" color="#666" @click="closeMask" />
        </div>
        <div class="mask-title">請輸入驗(yàn)證碼</div>
        <van-password-input
          :value="invitationCode"
          :length="6"
          :gutter="10"
          :mask="false"
          :error-info="errorInfo"
          :focused="showKeyboard"
          @focus="showKeyboard = true"
        />
        <div v-if="showLoading" class="code-loading"><van-loading /></div>
        <van-number-keyboard
        :maxlength="6"
        :show="showKeyboard"
         @input="onInput"
         @delete="onDelete"
         @blur="showKeyboard = false"
        />
    </div>
</template>
2.script
export default {
    data() {
      return {
        showMask: false,  //模態(tài)框展示
        showLoading: false,//驗(yàn)證loading 
        invitationCode: '', // 驗(yàn)證碼
        errorInfo: '', // 錯誤提示信息
        showKeyboard: false, // 數(shù)字鍵盤展示
      }
  },
  watch: {
    invitationCode: {
      handler(newVal, oldVal) {
        if (newVal.length === 6) {
          console.log('監(jiān)聽輸入完成', newVal)
          this.showKeyboard = false
          this.showLoading = true
          this.errorInfo = ''
          const params = {
            authCode: newVal
          }
          verifyCode(params).then(res => {  //后臺接口校驗(yàn)是否正確
            if (res.success) {
              this.$toast.success('驗(yàn)證成功')
              this.showMask = false
              this.showKeyboard = false
              //...驗(yàn)證成功執(zhí)行后續(xù)操作
            } else {
              this.errorInfo = '驗(yàn)證碼錯誤,請重試'
              this.invitationCode = ''
              this.showLoading = false
            }
          })
        }
      }
    }
  },
  methods:{
    //驗(yàn)證碼刪除
    onDelete() {
        const arr = this.invitationCode.split('')
        if (arr.length) {
          arr.pop()
          this.invitationCode = arr.join('')
        }
    },
  //展示模態(tài)框
  showMask(){
      this.invitationCode = ''
      this.errorInfo = ''
      this.showMask = true
      this.showKeyboard = true
      this.showLoading = false
  },
  //關(guān)閉模態(tài)框
    closeMask() {
      this.showKeyboard = false
      this.showMask = false
    }
  }
}
3.style
    .mask-box{
      position: fixed;
      left:0;
      right:0;
      top:0;
      bottom:0;
      background-color: rgba(0,0,0,.7);
      .context-box{
        width:90%;
        background: #fff;
        border:1px solid #fd0;
        height:280px;
        width: 320px;
        margin:auto;
        margin-top:44px;
        border-radius: 20px;
        padding: 10px;
        box-sizing: border-box;
        overflow-y: auto;
        .close-img{
          width:20px;
          height:20px;
        }
        .mask-title{
          color:#333;
          font-size: 16px;
          font-weight: bold;
          text-align: center;
          margin:30px 0 50px 0;
        }
        .code-loading{
          text-align: center;
          margin-top:10px;
        }
        .van-password-input__item{
          background: #f5f5f5 !important;
        }
      }
    }
    .van-number-keyboard{
      bottom: 30px;
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容