微信程序实现点击生成

微信小程序实现点击生成随机验证码

编程开发 2020-10-15 07:29:10 28

导读

本文实例为大家分享了微信小程序实现点击生成随机验证码的具体代码,供大家参考,具体内容如下效果图wxml<view bindtap="getCode">{{ code }}</view>jsPage({ data: { code: '' }, onLoad() {  ……

本文实例为大家分享了微信小程序实现点击生成随机验证码的具体代码,供大家参考,具体内容如下

效果图

微信小程序实现点击生成随机验证码

wxml

<view bindtap="getCode">{{ code }}</view>

js

Page({
data: {
 code: ''
},
onLoad() {
 //进入页面就调用方法 创建一个随机验证码
 this.createCode()
},

getCode() {
 //点击调用创建验证码方法
 this.createCode()
},

//创建随机验证码方法
createCode() {
 let code = ''; //初始化验证码
 let codeLength = 6; //设置验证码长度
 //设置随机字符
 let txt = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
 //循环
 for(let i = 0; i < codeLength; i++) {
 //设置随机数范围
 let index = Math.floor(Math.random() * 36);
 code += txt[index];
 }
 //动态设置数据
 this.setDate({
 code
 }) 
}
})


1253067 TFnetwork_cn