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

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项目极限优化全攻略
小程序·uni-app
CHU7290357 小时前
淘宝扭蛋机抽盒小程序前端功能解析:解锁趣味抽盒新体验
前端·小程序
2501_933907218 小时前
深圳本凡科技专业企业APP开发,助力手机应用创新优化
科技·微信小程序·小程序
每天都要加油呀!10 小时前
TypeError: uni.requestPayment is not a function
小程序
java1234_小锋10 小时前
分享一套优质的微信小程序校园志愿者系统(SpringBoot后端+Vue3管理端)
微信小程序·小程序·校园志愿者
2501_9160088912 小时前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone
打破砂锅问到底00712 小时前
AI 驱动开发实战:10分钟从零构建「微信群相册」小程序
人工智能·微信·小程序·ai编程
CHU72903512 小时前
扭蛋机盲盒小程序前端功能设计解析:打造趣味与惊喜并存的消费体验
前端·小程序
QT.qtqtqtqtqt13 小时前
uni-app小程序前端开发笔记(更新中)
前端·笔记·小程序·uni-app
CHU72903514 小时前
直播商城APP前端功能全景解析:打造沉浸式互动购物新体验
java·前端·小程序