0

我如何将两个函数与一个运算符OR ( || ) 结合起来我想在脚本中结合 2 个 ifs 语句 是小提琴

HTML

<form name="formName" method="post" action="#">
<input type="text" name="attendant[email]" size="13" value="">  
<input type="text" name="attendant[email]" size="13" value="">
<input type="text" name="attendant[email2]" size="13" value="">
<input type="text" name="attendant[email2]" size="13" value="">
<input type="submit" name="submit"> 
</form>

脚本

$("form").submit(function(event) {
    if ($("input:text[name='attendant\\[\email\]']", this).filter(function() {
            return $.trim(this.value) == "";
        }).length) {
        window.alert("No member email1 should be empty.");
        event.preventDefault();
    }
        if ($("input:text[name='attendant\\[\email2\]']", this).filter(function() {
            return $.trim(this.value) == "";
        }).length) {
        window.alert("No member email2 should be empty.");
        event.preventDefault();
    }
});
4

2 回答 2

0

像这样的东西:

$("form").submit(function(event) {
    var email1Validation = function() {
        if($("input:text[name='attendant\\[\email\]']", this).filter(function() {
            return $.trim(this.value) == "";
        }).length) {
            window.alert("No member email1 should be empty.");
            return true;
        }
    },
    email2Validation = function() {
        if ($("input:text[name='attendant\\[\email2\]']", this).filter(function() {
            return $.trim(this.value) == "";
        }).length) {
            window.alert("No member email2 should be empty.");
            return true;
        }
    }
    if(email1Validation() || email2Validation()) {
        event.preventDefault();
    }
});
于 2012-11-15T10:01:35.900 回答
0
$("form").submit(function(event) {
    if ($("input:text[name='attendant\\[\email\]']", this).filter(function() {
            return $.trim(this.value) == "";
        }).length) {
        window.alert("No member email1 should be empty.");

        event.preventDefault();
    }
       else if ($("input:text[name='attendant\\[\email2\]']", this).filter(function() {
            return $.trim(this.value) == "";
        }).length) {
        window.alert("No member email2 should be empty.");

        event.preventDefault();
    }
});

这是你需要做的..希望它可以帮助你。

于 2012-11-15T10:04:26.987 回答