javascript - 无法读取数组的属性

标签 javascript jquery arrays oop dynamic

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

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

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

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

当它运行到底部的 for 循环时,我不断收到错误。它说“未捕获的类型错误”无法读取未定义的属性“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";
};

最佳答案

由于qtest看起来像一个数组,它的索引从0开始到length - 1所以当jlength 时,qtest[j] 的值将是未定义的。

因此将循环更改为

for (j = 0; j< qtest.length; j++) {
    //
}

关于javascript - 无法读取数组的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25050556/

相关文章:

php - 使用 PHP 合并数组

javascript - 递归函数不超过调用栈

javascript比较运算符无法正确评估用户输入

Javascript:在 <br> 之前和之后向内容添加元素

php - 以日期格式为键对多维数组进行排序

arrays - OpenCL 2D 工作组尺寸

javascript - 由于应用程序路径不正确,Electron 应用程序自动启动会显示一个额外的窗口

javascript - 如何为在客户端检查密码的用户创建登录页面

javascript - Joomla 模块冲突

jquery - 我可以同时使用 .find() 和 .filter() 吗?