原生微信小程序,实现方格验证码输入框

WXML


			
				
				
		

JS

  data: {
        carNumerArr: [
            { value: "", focus: false, },
            { value: "", focus: false, },
            { value: "", focus: false, },
            { value: "", focus: false, },
            { value: "", focus: false, },
            { value: "", focus: false, },
        ],
        verificationCode:"",
    },
    handleInput(e) {
        const that = this
        const index = e.currentTarget.dataset.index //下标
        const value = e.detail.value //值
        let carNumerArr = that.data.carNumerArr
        carNumerArr[index].value = value;
        if (value) {
            for (let i = 0; i < carNumerArr.length; i++) {
                carNumerArr[i].focus = false
            }
            if (index == carNumerArr.length - 1) {
                carNumerArr[index].focus = true
            } else {
                carNumerArr[index + 1].focus = true
            }
        } else {
            console.log('删除')
            carNumerArr[index].value = ''
            for (let i = 0; i < carNumerArr.length; i++) {
                carNumerArr[i].focus = false
            }
            if (index !== 0) {
                carNumerArr[index - 1].focus = true
            } else {
                carNumerArr[0].focus = true
            }
        }
        that.setData({
            carNumerArr: carNumerArr
        })
        let verCode = []
        for(let i =0;i < that.data.carNumerArr.length;i++){
            verCode[i] = that.data.carNumerArr[i].value
        }
        console.log(verCode)
        const str = verCode.join('');
        console.log(str)
        that.setData({
            verificationCode:str
        })
    },

你可能感兴趣的:(微信小程序,小程序)