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

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
        })
    },
相关推荐
Dragon Wu2 小时前
taro 小程序 CoverImage Image src无法显示图片的问题
javascript·小程序·前端框架·taro
冷子夜3 小时前
uniapp 小程序 CSS 实现多行文本展开收起 组件
css·小程序·uni-app
说私域3 小时前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的社群构建与新型消费迎合策略研究
人工智能·小程序·开源·零售
说私域14 小时前
开源AI大模型等“神秘组合”,如何颠覆零售业数字化转型?
人工智能·小程序·开源·流量运营·零售
weixin_1772972206919 小时前
旧物回收小程序,一键解决旧物处理难题
小程序
*拯1 天前
小程序弹出层/抽屉封装 (抖音小程序)
小程序
半兽先生1 天前
uniapp小程序获取手机设备安全距离
小程序·uni-app
weixin_177297220691 天前
盲盒一番赏小程序系统发展:创新玩法激发市场活力
小程序
ywyy67981 天前
推客小程序系统开发:全栈式技术解决方案与行业赋能实践
大数据·人工智能·微信小程序·小程序·系统·推客系统·推客小程序
向明天乄1 天前
uniapp,小程序中实现文本“展开/收起“功能的最佳实践
小程序·uni-app