我有三个if语句。我需要另一个与这三个相关的if语句,如下所示:
如果(这三个if语句返回true){
ajax发送
} else {
不要寄
}。
如果你们想说出你对我的代码的建议,我会很高兴。
$(document).ready(function(){
$("#name").focusout(function() {
var name = $('#name').val();
name = $.trim(name);
if (name == '') {
$('#arrow_box1').css({
"visibility": "visible"
});
} else {
$('#box1').css({
"visibility": "hidden"
});
}
});
$("#email").focusout(function() {
var email = $('#email').val();
email = $.trim(email);
var validateEmail = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
if (email == '' || !validateEmail.test(email)) {
$('#box2').css({
"visibility": "visible"
});
} else {
$('#box2').css({
"visibility": "hidden"
});
}
});
$("#password").focusout(function() {
var password = $('#password').val();
password = $.trim(password);
var passlength = password.length;
if (password == '' || passlength < 6) {
$('#box3').css({
"visibility": "visible"
});
} else {
$('#box3').css({
"visibility": "hidden"
});
}
});
$('#submit-btn').click(function(){
var loginform = $('#register-form').serialize();
$.ajax({
type: "POST",
url: "ajax/ajaxregister.php",
data: $('#register-form').serialize(),
success: function(c) {
if(c == "ok") {
window.location.href = "homepage.php";
} else {
alert('log in failed!');
}
}
});
});
});
答案 0 :(得分:1)
在文档就绪后添加验证程序缓存
$(document).ready(function(){
var vaildator = {
isNameError: undefined,
isEmailError: undefined,
isPasswordError: undefined
};
然后,在三个if语句中将它们设置为true
或false
。
if (name == '') {
vaildator.isNameError = true;
$('#arrow_box1').css({
"visibility": "visible"
});
} else {
vaildator.isNameError = false;
$('#box1').css({
"visibility": "hidden"
});
}
最后,在你的ajax调用之前添加if
if (vaildator.isNameError === false &&
vaildator.isEmailError === false &&
vaildator.isPasswordError === false) {
$.ajax({