JQUERY:使用OR运算符组合2个函数

时间:2012-11-15 09:41:04

标签: javascript jquery

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

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>

SCRIPT

$("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();
    }
});

2 个答案:

答案 0 :(得分: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();
    }
});

答案 1 :(得分: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();
    }
});

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