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

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
        })
    },
相关推荐
万岳科技系统开发16 小时前
付费知识系统源码的整体架构设计与模块划分
java·数据库·小程序
2501_9159214316 小时前
iOS App 中 SSL Pinning 场景下代理抓包失效的原因
android·网络协议·ios·小程序·uni-app·iphone·ssl
一品威客爱开发17 小时前
情绪识别技术优化客服 App 体验:让服务更贴心高效!
小程序
Brduino脑机接口技术答疑20 小时前
SSVEP 脑电小程序说明
人工智能·机器学习·小程序·脑机接口
hello kitty w20 小时前
1.标签定义
小程序
说私域20 小时前
点状打法在电商与社群空间中的创新实践:基于开源链动2+1模式AI智能名片S2B2C商城小程序的探索
人工智能·小程序·开源
OctShop大型商城源码20 小时前
免费开源大型多用户多商家小程序电商系统源码_OctShop
微信小程序·小程序·开源·小程序电商系统·多用户商城系统
2501_9160088920 小时前
在 Windows 上使用开心上架(Appuploader)在 Windows 环境下创建与管理 iOS 证书
android·ios·小程序·https·uni-app·iphone·webview
小白变怪兽21 小时前
微信小程序页面中监听globalData数据变化
微信小程序·小程序
计算机毕设指导621 小时前
基于微信小程序的烧烤店点餐和结账系统【源码文末联系】
java·spring·微信小程序·小程序·tomcat·maven·intellij-idea