-1

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

目前它正在以相反的方式进行操作,例如它声明我需要选择至少一个答案,然后当它被排序时,它声明我需要添加其余的问题。

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;
}
4

1 回答 1

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/>';
    }
}

这是你要找的吗?

于 2013-02-08T16:23:46.557 回答