无法读取数组的属性

时间:2014-07-31 03:49:59

标签: javascript jquery arrays oop dynamic

我有一个非常复杂的页面,它使用动态信息生成一个包含正确相关信息的布局。

我将数据存储为对象。我基本上有15个对象,其中包含多个用户提交数据字段。

所有内容都在页面上正确存储和输出,但是现在我正在尝试验证用户尝试从编辑页面编辑它时的信息。这些信息都是正确生成和布局的,但是我在信息验证方面遇到了同样的错误。

验证应该通过并确定字段是否填写正确,如果它没有记录变量并将其添加到警报变量。然后,一旦我完成运行验证功能,它应该弹出一个警告,其中仍需要填写哪些字段。

当它通过for循环向底部运行时,我一直收到错误。它说'Uncaught TypeError'无法读取未定义的属性'questionNumber'。

在下面的代码上方,我存储了对象和属性,但是这个函数是一切都出错的地方。请注意,qtest对象中还有15个数组,但为了简化起见,我删除了所有数组。

我已经将它用于更小,更简单的表单,但是由于复杂性和存储方法,我认为这可能会丢失一些东西,或者我可能无法正确访问某些内容。代码很长很低,我尽可能地缩减了。如果您有任何见解或帮助,请提供,我将非常感激。谢谢!

var validateQ = function(qTextID, qAnswerType, TFID, MCID, MCText1, MCText2, MCText3, MCText4, VisRef, Youtube, Vimeo, ImgID) {
    if (document.getElementById('ItemName').value == "") {
        var quizName = true;
    };
    if (jQuery('select[name="CAT_Custom_14"]').val() == 'Quiz') {
        if (jQuery(qTextID).val() == "") {
            var qText = true;
        };
        if (jQuery('CAT_Custom_249').val() == " ") {
            var quizscore1 = true;
        };
        if (jQuery(qAnswerType).val() == " ") {
            var answertype = true;
        } else if (jQuery(qAnswerType).val() == 'True/False') {
            if (!jQuery(TFID).is(':checked')) {
                var tfanswer = true;
                var mcanswer = false;
            };
        } else if (jQuery(qAnswerType).val() == 'Multiple Choice') {
            if (!jQuery(MCID).is(':checked')) {
                var mcanswer = true;
                var tfanswer = false;
            };
                if (jQuery(MCText1).val() == "" || jQuery(MCText2).val() == "" || jQuery(MCText3).val() == "" || jQuery(MCText4).val() == "") {
                    var mcTextfields = true;
                } else {
                    mcTextfields = false;
                };
            };
    } else if (jQuery('select[name="CAT_Custom_14"]').val() == 'Survey') {
        if (jQuery(qTextID).val() == "") {
            var qText = true;
        };
        if (!jQuery(sAnswers1).is(':checked')) {
            var surveyAnswers1 = true;
        } else {
            surveyAnswers1 = false;
        };
    };
        if (jQuery(VisRef).val() != " ") {
            if (jQuery(VisRef).val() == "Youtube Video" && jQuery(Youtube).val() == "") {
                var youtubeVal = true;
            } else if (jQuery(VisRef).val() == "Vimeo Video" && jQuery(Vimeo).val() == "") {
                var vimeoVal = true;
            } else {
                //               validateImage(ImgID);
            };
        } else {
            youtubeVal = false;
            vimeoVal = false;
            var tempImgCheck = false;
        };
        if (numCheck == 15) {
            numCheck = 16;
        };
    var qName = "- Quiz or Survey Name\n";
    var shortDescription = "- A short description of the Quiz/Survey\n";
    var scoreMessage = "- A required passing score\n";
    var QTextMessage = "- Question text\n";
    var answerTMessage = "- An answer type\n";
    var mcFields = "- The Multiple Choice answer fields\n";
    var mcMessage = "- The correct Multiple Choice Answer\n"
    var tfMessage = "- The correct True/False answer\n";
    var vimMessage = "- A Vimeo Video code\n";
    var ytMessage = "- A Youtube Video code\n";
    var imgMessage = "- A reference image\n";
    var surveyMessage = "- An answer type\n";
    if (quizName == true || quizscore1 == true || qText == true || answertype == true || tfanswer == true || mcanswer == true || mcTextfields == true || youtubeVal == true || vimeoVal == true || tempImgCheck == true || surveyAnswers1 == true) {
        var alertText = "It appears that you have not finished completing question" + question[i].questionNumber + ". Please ensure that you have completed the following question fields.\n";
        if (quizName == true) {
            alertText = alertText + qName;
        };
        if (quizscore1 == true) {
            alertText = alertText + scoreMessage;
        };
        if (qText == true) {
            alertText = alertText + QTextMessage;
        };
        if (answertype == true) {
            alertText = alertText + answerTMessage;
        };
        if (tfanswer == true) {
            alertText = alertText + tfMessage;
        };
        if (mcanswer == true) {
            alertText = alertText + mcMessage;
        };
        if (mcTextfields == true) {
            alertText = alertText + mcFields;
        };
        if (youtubeVal == true) {
            alertText = alertText + ytMessage;
        };
        if (vimeoVal == true) {
            alertText = alertText + vimMessage;
        };
        if (tempImgCheck == true) {
            alertText = alertText + imgMessage;
        };
        if (surveyAnswers1 == true) {
            alertText = alertText + surveyMessage;
        };
        if (quizscore1 == true) {
            alertText = alertText + scoreMessage;
        };
        confirm(alertText);
    };
};

    var numCheck = 1;
var checkQuizQ = function() {
    for (j = 1; j<= qtest.length; j++) {
        numCheck = numCheck + 1;
        if (qtest[j].questionNumber == "1") {
            validateQ("CAT_Custom_3", "CAT_Custom_8", "CAT_Custom_19", "CAT_Custom_18", "CAT_Custom_4", "CAT_Custom_5", "CAT_Custom_6", "CAT_Custom_7", "CAT_Custom_9", "CAT_Custom_10", "CAT_Custom_11", "CAT_Custom_12", "CAT_Custom_230");
        } else if (qtest[j].questionNumber == "2") {
            validateQ("CAT_Custom_20", "CAT_Custom_21", "CAT_Custom_29", "CAT_Custom_26", "CAT_Custom_22", "CAT_Custom_23", "CAT_Custom_24", "CAT_Custom_25", "CAT_Custom_30", "CAT_Custom_31", "CAT_Custom_32", "CAT_Custom_33", "CAT_Custom_231");
        } else if (qtest[j].questionNumber == "3") {
            validateQ("CAT_Custom_35", "CAT_Custom_36", "CAT_Custom_37", "CAT_Custom_40", "CAT_Custom_41", "CAT_Custom_42", "CAT_Custom_43", "CAT_Custom_44", "CAT_Custom_45", "CAT_Custom_46", "CAT_Custom_47", "CAT_Custom_48", "CAT_Custom_232");
        } else if (qtest[j].questionNumber == "4") {
      };
    };
document.getElementById('catcustomcontentbutton').style.display = "block";
document.getElementById("qsValidate").style.display = "none";
};

1 个答案:

答案 0 :(得分:1)

由于qtest看起来像一个数组,因此其索引从0开始到length - 1,因此当jlengthqtest[j]的值为for (j = 0; j< qtest.length; j++) { // } 将是未定义的。

因此将循环更改为

{{1}}