【问题标题】:Property of an array cannot be read无法读取数组的属性
【发布时间】:2014-09-22 21:44:03
【问题描述】:

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

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

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

验证应该通过并确定字段是否填写正确,以及是否未记录变量并将其添加到警报变量中。然后,一旦我完成了验证功能的运行,它应该会弹出一个警报,其中包含哪些字段仍需要填写。

当它通过 for 循环向底部运行时,我不断收到错误消息。它说“未捕获的类型错误”无法读取未定义的属性“问题编号”。

在下面的代码中,我存储了对象和属性,但是这个函数是一切都出错的地方。请注意,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";
};

【问题讨论】:

  • 你是说j吗?在for循环中?我在这段代码上方还有另一个 for 循环,你看不到,它使用 i 变量。

标签: javascript jquery arrays oop dynamic


【解决方案1】:

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

所以把循环改成

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

【讨论】:

  • 我其实是从1开始qtest数组。
  • @user3593878 - 常规数组总是从 0 开始,一直到它们的 length - 1 - 您可以选择从索引 1 显式存储值 - 但它不会改变它们从0.
  • 嗯 - 所以这可能是错误的原因?即使我的数组和 for 循环都查找从 1 开始的值?
  • @user3593878 数组中的最后一项仍将具有索引qtest.length - 1,因此请尝试for (j = 1; j &lt; qtest.length; j++) {}
  • @user3593878 你有一个ivar alertText = "It appears that you have not finished completing question" + question[i].questionNumber + ". Please ensure that you have completed the following question fields.\n";
猜你喜欢
  • 2013-02-21
  • 2018-07-05
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 2021-01-14
  • 1970-01-01
相关资源
最近更新 更多