如何正确地订购验证检查

时间:2013-02-05 15:12:47

标签: jquery

我的验证存在轻微问题。我想要发生的是,我想通过在底部显示验证消息来检查是否首先添加了所有行,说明我还有很多问题。只有当所有问题都已添加并且验证通过后,它才会显示每个问题的验证,例如请选择至少一个答案,或者您选择的答案少于所需的答案。

目前正在反过来这样做,例如它说我需要选择至少一个答案,然后当它被排序时,它表明我需要添加其他问题。

function validation() {

    var marks = parseInt($("#total-weight").text());
    var _qid = "";
    var _msg = "";

    var maxQuestions = <? php echo(int) $_SESSION['textQuestion']; ?> ;
    var questionsAdded = $('tr.optionAndAnswer').length;

    var alertValidation = "";
    // Note, this is just so it's declared...
    $("tr.optionAndAnswer").each(function () {

        _qid = $("td.qid", this).text();
        _msg = "You have errors on Question Number: " + _qid + "\n";

        $(".numberAnswerTxtRow", this).each(function () {
            var currenttotal = $(this).closest('.optionAndAnswer').find('.answerBtnsOn').length;

            if ($(this).val() == 0) {
                alertValidation += "\n\u2022 You have not selected an answer, please select at least one answer\n";
            } else if (currenttotal < $(this).val()) {
                alertValidation += "\n\u2022 You have selected less answers than the required amount\n";
            }

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

    });



    if (alertValidation == "") {
        if (questionsAdded < maxQuestions) {
            _msg = '';
            alertValidation = ("You Have Not Added in All of Your Questions \n\n\u2022 You have " + (maxQuestions - questionsAdded) + " Questions Remaining:");
        }
    }

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

    return true;
}

以下是整个代码 - 在下面的代码中,在第一次传递剩余验证的问题数量之前,它不会显示其他验证消息。我提到的唯一问题是$(".numberAnswerTxtRow",this).each(function() {中的验证,它也是包含else的其他函数中唯一的函数if:

function validation() {

    var marks = parseInt($("#total-weight").text());
    var _qid = "";
    var _msg = "";

    var maxQuestions = <? php echo(int) $_SESSION['textQuestion']; ?> ;
    var questionsAdded = $('tr.optionAndAnswer').length;

    var alertValidation = "";
    // Note, this is just so it's declared...
    $("tr.optionAndAnswer").each(function () {


        _qid = $("td.qid", this).text();
        _msg = "You have errors on Question Number: " + _qid + "\n";

        $(".textAreaQuestion", this).each(function () {



            if (!this.value || this.value.length < 5) {
                alertValidation += "\n\u2022 You have not entered a valid Question\n";
            }

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

        $(".numberAnswerTxtRow", this).each(function () {


            var currenttotal = $(this).closest('.optionAndAnswer').find('.answerBtnsOn').length;

            if ($(this).val() == 0) {
                alertValidation += "\n\u2022 You have not selected an answer, please select at least one answer\n";
            } else if (currenttotal < $(this).val()) {
                alertValidation += "\n\u2022 You have selected less answers than the required amount\n";
            }

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

        });

        $(".txtWeightRow", this).each(function () {


            if (!this.value) {
                alertValidation += "\n\u2022 Please enter in a figure for Number of Marks for this Question\n";
            }

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


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

    if (alertValidation == "") {

        if (questionsAdded < maxQuestions) {
            _msg = '';
            alertValidation = ("You Have Not Added in All of Your Questions \n\n\u2022 You have " + (maxQuestions - questionsAdded) + " Questions Remaining:");
        }



    }

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

    return true;
}

1 个答案:

答案 0 :(得分:1)

如果我的问题得到了解决,那就是你需要做的事情:

$questions = array();

while ($selectedstudentanswerstmt->fetch()) {

    //Check if the student data exist.
    if (!isset($questions[$detailsStudentId])) {
        $questions[$detailsStudentId] = array(
            'studentalias' => $detailsStudentAlias,
            'studentforename' => $detailsStudentForename,
            'studentsurname' => $detailsStudentSurname,
            'questions' => array();
        );
    }

    $questions[$detailsStudentId]['questions'][$detailsQuestionId] = array(
        'questionno'=>$detailsQuestionNo,
        'content'=>$detailsQuestionContent,
        'optiontype'=>$detailsOptionType,
        'noofanswers'=>$detailsNoofAnswers,
        'answer'=>$detailsAnswer,
        'replytype'=>$detailsReplyType,
        'questionmarks'=>$detailsQuestionMarks,
        'studentanswer'=>$detailsStudentAnswer,
        'responsetime'=>$detailsResponseTime,
        'mouseclick'=>$detailsMouseClick,
        'studentmark'=>$detailsStudentMark
    );

}

然后你可以很容易地迭代数组:

foreach ($questions as $studentId => $studentData) {
    echo $studentData['studentAlias'].' - '.$studentData['studentforename'].' '.$studentData['studentsurname'].'<br/>';

    foreach ($studentData['questions'] as $questionId => $questionData) {
        echo '<strong>'.$questionData['questionno'].': '.$questionData['content'].'<br/>';
        echo $questionData['optiontype'].' - '.$questionData['noofanswers'].' - '.$questionData['answer'].' - '.$questionData['replytype'].' - '.$questionData['questionmarks'].'<br/>';
        echo $questionData['studentanswer'].' - '.$questionData['responsetime'].' - '.$questionData['mouseclick'].' - '.$questionData['studentmark'].'<br/>';
    }
}

这是你要找的吗?