如图所示
js:
formSubmit: function(e){
if(e.detail.value.code=''){
wx.showToast({
title: '请输入验证码',
icon:'none'
})
}
//将输入的验证码和生成的验证码都转为全大写字母,然后再比较是否相等
else if(e.detail.value.code.toUpperCase()==this.code.toUpperCase()){
console.log('验证码输入正确')
}
},
已经解决了。
输入框: //获取输入验证码 makecodeInput: function (e) { console.log("用户输入的验证码",e.detail.value); this.setData({ makecode: e.detail.value }) }, 验证是这样子: if(this.data.makecode != this.data.code){ wx.showToast({ title: '验证码不正确', icon: 'none', duration: 2000 }) return }
e 是从哪里获取的,e是undefined 才报的错
首先判断条件用 == 或者 === 而不是 = ,其次你的代码e.detail.value能否取到值? if(!e.detail.value.code){ wx.showToast({ title: '请输入验证码', icon:'none' }) }
changeCode(e){
var code;
code = '';
var codeLength = 4;
var random = 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', '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 (var i = 0; i < codeLength; i++){
var index = Math.floor(Math.random() * 62);
code += random[index];
}
this.setData({
code: code
})
},
在控制台打印下e的值