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

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
        })
    },
相关推荐
笨笨狗吞噬者2 小时前
【uniapp】微信小程序实现自定义 tabBar
前端·微信小程序·uni-app
2501_933907212 小时前
如何选择性价比高的宁波小程序开发服务公司?
科技·微信小程序·小程序
阿珊和她的猫4 小时前
微信小程序 WXSS 与 CSS 的区别
css·微信小程序·notepad++
nhc0886 小时前
贵阳纳海川科技・棋牌室行业数字化解决方案
科技·微信小程序·小程序·软件开发·小程序开发
lizi667 小时前
uniapp uview-plus 自定义动态验证
前端·vue.js·微信小程序
2501_915909067 小时前
iOS 抓包不越狱,代理抓包 和 数据线直连抓包两种实现方式
android·ios·小程序·https·uni-app·iphone·webview
CHU7290358 小时前
让知识传递更顺畅:在线教学课堂APP的功能设计
前端·人工智能·小程序
AI前端老薛8 小时前
小程序中简单 Loading 效果关键帧动画
小程序
Greg_Zhong9 小时前
小程序从搭建到开发,涉及基础及必备知识总结
微信小程序
Greg_Zhong9 小时前
小程序中实现左侧分类与右侧子分类的联动效果.....
小程序·左侧分类与右侧分类联动