笔者今天在做微信小程序项目时,涉及到两个表单提交,提交前进行判断的问题。两个表单中重合的参数有2个,在这种情况下,写判断函数时采用可选参数。
在写函数的时候,我们有时候会设置可选参数,可选参数可以这样写:
this.telephone_numbe = telephone_numbe || 0; // 可选参数
完整代码如下:
showTopTips: function(name, id_number, telephone_number) {
this.telephone_numbe = telephone_numbe || 0; // 可选参数
var that = this;
if (name == '') {
that.setData({
showTopTips: true,
stop: 1,
info: '真实姓名未填写!'
});
setTimeout(function() {
that.setData({
showTopTips: false
});
}, 3000);
} else if (id_number == '') {
that.setData({
showTopTips: true,
stop: 1,
info: '身份证号未填写!'
});
setTimeout(function() {
that.setData({
showTopTips: false
});
}, 3000);
} else if (telephone_number == '') {
that.setData({
showTopTips: true,
stop: 1,
info: '手机号码未填写!'
});
setTimeout(function() {
that.setData({
showTopTips: false
});
}, 3000);
} else {
that.setData({
stop: 0,
showTopTips: false
});
}
},
下面两个表单提交时,都可以使用 showTopTips( )
函数来进行判断。
提交表单信息-1 :包含3个参数
// 提交表单信息-1
formSubmit: function(e) {
this.showTopTips(e.detail.value.name, e.detail.value.id_number, e.detail.value.telephone_number);
if (this.data.stop == 1) {
console.log('用户输入错误!')
} else {
......
}
提交表单信息-2 :只包含2个参数。此时,可选参数 telephone_number
使用默认值。
// 提交表单信息-2
formSubmit: function(e) {
this.showTopTips(e.detail.value.name, e.detail.value.id_number);
if (this.data.stop == 1) {
console.log('用户输入错误!')
} else {
......
}
作者:戴翔
电子邮箱:[email protected]
简介:中华人民共和国公民,中国共青团员,CSDN博客专家,秦淮区疾控中心托管社会公益组织指南针工作室志愿者,创业公司研发中心负责人,在校大学生。