如何在一个警报中显示所有验证消息

时间:2012-12-17 19:19:21

标签: jquery

在我的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;
}​

2 个答案:

答案 0 :(得分:0)

删除此代码块

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

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

答案 1 :(得分:-1)

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

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

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

alert(alertValidation1 + '\n' + alertValidation2 + '\n' ...etc)