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

WXML

JS 复制代码
<view class="input_box">
			<block wx:for="{{carNumerArr}}" wx:key="index" wx:for-index="index">
				<input class="grid-input" type="number" maxlength="1" bindinput="handleInput" data-index="{{index}}" focus="{{item.focus}}" value="{{item.value}}" />
				</block>
		</view>

JS

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
        })
    },
相关推荐
好赞科技1 分钟前
2026年高口碑餐厅预约小程序排行榜:智能就餐新体验一键解锁
大数据·微信小程序
万岳软件开发小城3 小时前
陪诊APP+小程序一体化搭建方案:如何低成本打造医疗陪护平台?
小程序·医院陪诊系统源码·陪诊软件开发·陪诊平台开发·陪诊小程序开发
lichenyang4537 小时前
Expo 小程序媒体库功能设计与实现记录
小程序
经济元宇宙11 小时前
2026混合开发工具选型:小程序生态适配测评
小程序
lpfasd1231 天前
微信小程序虚拟支付(道具直购)踩坑全记录:从-15005到支付成功
微信小程序·小程序
crazy_wsp1 天前
使用AI从0到1上线微信小程序
人工智能·微信小程序·小程序
小宋的踩坑日记1 天前
全网最全!Tailwind/Unocss 类名速查表,前端开发必备神器!
css·小程序·前端框架
低代码布道师1 天前
健身房私教课小程序需求规格说明书
小程序·规格说明书
m0_462803882 天前
培训分组与记分操作指南
微信小程序
浩冉学编程2 天前
微信小程序中基于java后端实现官方的文本内容安全识别msgSecCheck
java·前端·安全·微信小程序·小程序·微信公众平台·内容安全审核