jQuery条件通过

jQuery conditions pass through

本文关键字:条件 jQuery      更新时间:2023-09-26

我为2个输入做了自己的表单验证,一个是电话号码,另一个是电子邮件地址。而且我在一页里有两张表格。

我的代码是

var email, phone;
if (email address validation passed) {
    email = true;
} else {
    email = false;
}
if (phone number validation passed) {
    phone = true;
} else {
    phone = false;
}
if (!(phone && email)) {
    return false
} else {
    return true
}

由于同一页面上有两个表单,所以我想为第二个表单添加另一个代码片段,例如

var email2, phone2;
if (email address validation passed) {
    email2 = true;
} else {
    email2 = false;
}
if (phone number validation passed) {
    phone2 = true;
} else {
    phone2 = false;
}
if (!(phone2 && email2)) {
    return false
} else {
    return true
}

我发现的问题是,为了提交表格,我需要让email, phone, email2, phone2;都等于true。但是,我需要在email, phone are truephone2, email2 are true条件下提交只是需要有人来检查这是否是解决我问题的正确逻辑方法?

if (!(phone2 && email2)) {
    return false
} else if(!(phone  && email )) {
    return false
} else return true;

但是,如果email, phone为真或者phone2, email2为真,我需要提交

你说的方式是最简单的编码方式:

if ((email && phone) || (email2 && phone2)) {
    return true;
} else {
    return false;
}

如果你只是想根据一个条件是真还是假返回真或假,你可以在一行中这样做:

return (email && phone) || (email2 && phone2);

你可以像

if((phone2 && email2) || (phone && email))
    return true;
else
    return false;