微信小程序做form表单提交,我在form组件的submit函数里调用每个input绑定的失焦事件,在提交表单前验证下每个input数据,但是发现直接调用失焦事件的话会报错,具体代码如下:
<form bindsubmit='submit'>
<input placeholder="请输入姓名" bindblur="checkName"/>
<input placeholder="请输入密码" bindblur="checkPassword"/>
<button form-type='submit'>提交</button>
</form>
Page({
data: {
name: '',
password: ''
},
checkName:function (e) {
//这里正则验证输入数据
console.log(e.detail.value);
if(验证通过){
return true;
}
return false;
},
checkPassword:function(e) {
//这里正则验证输入数据
console.log(e.detail.value);
if(验证通过){
return true;
}
return false;
},
submit:function (e) {
if (!this.checkName()) return
if (!this.checkPassword()) return
}
})
报错为:Cannot read property 'detail' of undefined;at pages/index/index page checkName function
TypeError: Cannot read property 'detail' of undefined
不在submit里调用那几个失焦函数的话,正常input失焦可以打印e.detail.value的,但为啥调了就报错了
你直接调用函数,this传不过去啊,就取不到用户名和密码的值
正确方法应该是把组件的value与data中的name和password进行双向绑定
检查的时候检查data里的name和password,这样不管在哪里调用函数都能正常运行