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

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_9159090613 小时前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张16 小时前
iOS加固技术路线全面解析:Bitcode模式、源码模式与汇编模式对比及爱加密优势
android·汇编·ios·小程序·uni-app·cocoa·iphone
海纳百川·纳海川17 小时前
租房行业数字化:换个思路解决“老问题”
大数据·微信小程序·小程序
上海云盾-小余1 天前
CC 高频请求 + DDoS 流量攻击实战防御:全链路 WAF 流量清洗部署完整流程
网络·爬虫·安全·小程序·ddos
2501_916008891 天前
iOS IPA文件反编译与打包操作方法,拆包分析防护和加固打包
android·macos·ios·小程序·uni-app·cocoa·iphone
2301_810313181 天前
海外教育APP定制发展现状与开发选型核心要点解析
小程序·团队开发·软件需求
2501_915921432 天前
iOS手游安全反外挂解决方案:基于Xcode的加固混淆特色详解
android·安全·ios·小程序·uni-app·iphone·xcode
禾高网络2 天前
回收小程序在环保和商业领域发挥更大的作用
java·大数据·人工智能·小程序
2501_916007472 天前
iOS App分发教程 App Store、TestFlight 与 Ad Hoc 的配置与上传方法
android·ios·小程序·https·uni-app·iphone·webview
JasonMa1532 天前
从零搭建一个微信小程序活动管理平台(一):架构设计与技术选型
javascript·微信小程序·node.js