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

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
        })
    },
相关推荐
2501_915909062 小时前
深入理解HTTPS中间人抓包技术原理与实战指南
网络协议·http·ios·小程序·https·uni-app·iphone
he___H2 小时前
微信小程序实现两行交错功能
微信小程序·小程序
前端小木屋18 小时前
uniapp与蓝牙设备连接详细步骤
前端·微信小程序
万岳科技系统开发21 小时前
私域直播系统开发从0到1:企业直播平台搭建全过程
前端·小程序·架构
2501_9160074721 小时前
iOS应用性能优化全面指南:从内存管理到工具使用
android·ios·性能优化·小程序·uni-app·iphone·webview
i220818 Faiz Ul1 天前
理财系统|基于java+vue的家庭理财系统小程序(源码+数据库+文档)
java·vue.js·spring boot·小程序·论文·毕设·理财系统
河北清兮网络科技1 天前
企业软件开发全流程:从需求到上线,如何高效落地?
小程序·app·短剧·短剧app·广告联盟
维双云1 天前
想做企业公司的教育知识付费小程序多少钱?
小程序
huang_jimei1 天前
【无标题】
微信小程序
柚鸥ASO优化1 天前
微信正在变成“搜索引擎”:小程序SEO机会全面爆发
搜索引擎·微信·小程序·小程序优化