返回 javascript 函数的真/假值,然后根据该值执行操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10789069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:56:01  来源:igfitidea点击:

Return true/false value of a javascript function and then do stuff based on that

javascriptjqueryformsvalidation

提问by izolate

I'm building a contact form and I need help with the jQuery validator.

我正在构建一个联系表单,我需要有关 jQuery 验证器的帮助。

function contactform() {
    $("form #submit").on("click", function() {
        $("form input").removeClass("error");
        validator();
        // 3rd action goes here
    });
});

validator()checks to see if any input is left empty, and if so it adds an error class to it:

validator()检查是否有任何输入为空,如果是,则向其添加一个错误类:

function validator() {
    $("form input").each(function() {
        var value = $(this).val();
        if (value.length <= 0) {
            $(this).addClass("error");
            return false;
        }
    });
});

Now, for the 3rd action in contactform()I want to say that if validator() = true(i.e. there no inputs that are empty), then continue on to the next code.

现在,对于第三个动作,contactform()我想说如果validator() = true(即没有为空的输入),则继续执行下一个代码。

I can't seem to return the value of validator(). Does anybody know the best way to do this?

我似乎无法返回validator(). 有人知道这样做的最佳方法吗?

采纳答案by VisioN

Here is another solution using filtermethod:

这是使用filter方法的另一种解决方案:

function validator() {
    return $("form input").filter(function() {
        return $.trim(this.value).length == 0;
    }).addClass("error").length == 0;
});

function contactform() {
    ...
    if (validator()) {
        // it's OK
    } else {
        // there are errors
    }
}

UPDATE: Awesomely updated with the helpof @am_not_i_am. Thanks!

更新:在@am_not_i_am的帮助下进行了惊人的更新。谢谢!

回答by Lucero

The problem you seem to encounter is that you have a nested function and closure, which prevents you from returning the value directly.

您似乎遇到的问题是您有一个嵌套的函数和闭包,这会阻止您直接返回值。

Something like that should do the trick:

这样的事情应该可以解决问题:

function validator() {
    var result=true;
    $("form input").each(function() {
        var value = $(this).val();
        if (value.length <= 0) {
            $(this).addClass("error");
            result = false;
        }
    });
    return result;
});

回答by thecodeparadox

function validator() {
    var result = true;
    $("form input").removeClass("error");
    $('form input').each(function() {
           if(!$.trim(this.value)) {
             $(this).addClass('.error');
             result = false;  
           }
     });
    return result;
}


function contactform() {
    $("form #submit").on("click", function() {
        if(validator()) { // pass the validation

        } else { // fail validation

        }
        // 3rd action goes here
    });
});