uni-app 踩坑之验证码多框输入

要实现的小程序页面如图:
在这里插入图片描述
页面代码:

<template>
	<view class="login-container">
		<view class="login-header">
			<view class="login-header-item">请输入验证码</view>
			<view class="font-color-9b9ba5 font-26">验证码已发送至手机</view>
			<view class="font-color-9b9ba5 font-26" style="margin-top: 12rpx;">12345678910</view>
		</view>
		<view>
			<view class="font-26 flex-between-center" style="margin-bottom: 30rpx;">
				<text class="font-color-9b9ba5">请输入6位数字验证码</text>
				<text class="font-color-0078FF">59s</text>
			</view>
			<view class="flex-between-center code-input-container" style="font-size: 42rpx;">
				<!-- 页面显示 -->
				<input
					type="number"
					v-for="(item, index) in code_len"
					:key="index"
					disabled
					@tap="onCode"
					:value="code_val.length >= index + 1 ? code_val[index] : ''"
					class="code-input-item flex-default-center"
				/>
			</view>
			<!-- 实际监听 -->
			<input v-if="is_focus" @blur="blurCode" @input="setCode" type="number" class="code-input" v-model="code_val" :maxlength="code_len" :focus="true"/>
		</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			code_len: 6, // 验证码长度
			code_val: '', // 验证码的值
			is_focus: false
		};
	},
	methods: {
		setCode(e) {
			this.code_val = e.detail.value;
		},
		onCode() {			
			this.is_focus = true;
		},
		blurCode() {
			this.is_focus = false;
		}
	}
};
</script>
<style lang="scss">
.font-26 {
	font-size: 26rpx;
}
.font-color-9b9ba5 {
	color: #9b9ba5;
}
.font-color-0078FF {
	color: #0078ff;
}
.login-container {
	padding: 0 30rpx;
}
.login-header {
	padding: 90rpx 0;
	.login-header-item {
		font-size: 42rpx;
		margin-bottom: 17rpx;
	}
}
.code-input {
	height: 0;
	width: 0;
}
.code-input-item {
	width: 100rpx;
	height: 100rpx;
	border: 1px solid #9b9ba5;
	justify-content: center;
	text-align: center;
}
</style>

重点注意 v-if 的绑定,由失焦隐藏、点击显示来控制手机实际键盘的弹出。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值