0

在我下面的 jquery 验证中,如果发生多个验证错误,我希望所有验证一次出现在警报中,而不是在警报中一次显示一个验证。如何在一个警报中显示所有消息?

    function validation() {

    var alertValidation = "";
    var _qid = "";
    var _msg = "";

    $("input[data-type='qmark']").each(function(i) {
        var questions = $(this).attr("data-qnum");
        var marks = parseInt($("[class*=q" + i + "_ans_text]").text());
        var txtinput = $(this).val();
        _qid = questions;
        _msg = "You have errors on Question Number: " + _qid + "\n";


        if (txtinput == '') {
            alertValidation += "\n\u2022 You have not entered in a value in all the Indivdiaul Marks textbox\n";
        }


        if (marks < '0') {

            alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks";
        }

        if (marks > '0') {

            alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks + " Marks Remaining";
        }

        if (alertValidation != "") {
            return false; //Stop the each loop 
        }

    });


    if (alertValidation != "") {
        alert(_msg + alertValidation);
        return false;
    }

    return true;
}​
4

2 回答 2

0

删除此代码块

if (alertValidation != "") {
    return false; //Stop the each loop 
}

您已经在连接错误消息,上面的代码行在遇到错误时会中断您的循环。所以删除它应该可以解决你的问题..

于 2012-12-17T19:27:17.497 回答
-1

对于每个警报验证,给它一个不同的名称:

if (txtinput == '') {
     alertValidation1 += 'blah blah'
}
if (marks > '0') {
     alertValidation2 += 'other stuff'
}

然后在警报结束时,执行以下操作:

alert(alertValidation1 + '\n' + alertValidation2 + '\n' ...etc)
于 2012-12-17T19:24:45.717 回答