【问题标题】:Trouble looping through an object name循环遍历对象名称时遇到问题
【发布时间】:2014-05-01 19:18:39
【问题描述】:

我是 javascript 新手,我正在尝试在业务催化剂中创建一个动态的测验问题列表。通过 BC 的设置方式,您可以使用 {tags} 在页面上放置用户创建的信息。

也就是说,我正在尝试根据用户选择的值生成测验问题列表。我创建了一个对象“问题”,并将必要的属性及其值放入下面新定义的对象中。

在我的代码中,我目前正在尝试:

1 - 定义问题对象类

2 - 定义 15 个可能的测验问题

3 - 编写一个 for 循环,该循环将根据前一个问题的键值编写每个问题。

我编写的函数/for 循环最终会这样做:

写第一题

如果 question1 键的值为“是”,则编写问题 2

如果问题 2 的键值为“是”,则写问题 3

等等,直到没有更多的“是”或第 15 题已经写完。

当我尝试执行下面的代码时,我收到错误“未捕获的 ReferenceError:问题未定义”有人可以帮助我了解我做错了什么吗?感谢您的所有帮助!

注意:为了节省空间,我只包含了三个问题变量。在我的实际代码中,我定义了 15 个问题对象。

<script>
    function Question (questionNumber, questionText, answerType, answerA, answerB, answerC, answerD, correctAnswer, visualRef, refKey, nextQ, qTextField, aTypeField, mcAField, mcBField, mcCField, mcDField, mcUserAnswer, tfUserAnswer, sRatings, sSAnswer, passFail) {
    this.questionNumber = questionNumber;
    this.questionText = questionText;
    this.answerType = answerType;
    this.answerA = answerA;
    this.answerB = answerB;
    this.answerC = answerC;
    this.answerD = answerD;
    this.true = "True";
    this.false = "False";
    this.correctAnswer = correctAnswer;
    this.visualRef = visualRef;
    this.refKey = refKey;
    this.nextQ = nextQ;
    this.qTextField = qTextField;
    this.aTypeField = aTypeField;
    this.mcAField = mcAField;
    this.mcBField = mcBField;
    this.mcCField = mcCField;
    this.mcDField = mcDField;
    this.mcUserAnswer = mcUserAnswer;
    this.tfUserAnswer = tfUserAnswer;
    this.sRatings = sRatings;
    this.sSAnswer = sSAnswer;
    this.passFail = passFail;
    this.createQuestion = function() {
            document.write("This is writing question " + this.questionNumber );
        };
    };

    var question1 = new Question("1", "{tag_q-question_1}", "{tag_q-answer-type_1}", "{tag_q-text-answer_101}", "{tag_q-text-answer_102}", "{tag_q-text-answer_103}", "{tag_q-text-answer_104}", "{tag_q-multichoice-answer_1}{tag_q-t/f-answer_1}", "{tag_q-visual-reference_1}", "{tag_q-youtube_1}{tag_q-vimeo_1}{tag_q-image_1_value}", "{tag_q-next-question_1}", "CAT_Custom_13", "CAT_Custom_11", "CAT_Custom_14", "CAT_Custom_15", "CAT_Custom_16", "CAT_Custom_17", "CAT_Custom_7", "CAT_Custom_8", "CAT_Custom_9", "CAT_Custom_10", "CAT_Custom_12");
    var question2 = new Question("2", "{tag_q-question_2}", "{tag_q-answer-type_2}", "{tag_q-text-answer_201}", "{tag_q-text-answer_202}", "{tag_q-text-answer_203}", "{tag_q-text-answer_204}", "{tag_q-multichoice-answer_2}{tag_q-t/f-answer_2}", "{tag_q-visual-reference_2}", "{tag_q-youtube_2}{tag_q-vimeo_2}{tag_q-image_2_value}", "{tag_q-next-question_2}", "CAT_Custom_19", "CAT_Custom_20", "CAT_Custom_22", "CAT_Custom_23", "CAT_Custom_24", "CAT_Custom_25", "CAT_Custom_21", "CAT_Custom_26", "CAT_Custom_27", "CAT_Custom_28", "CAT_Custom_29");
    var question3 = new Question("3", "{tag_q-question_3}", "{tag_q-answer-type_3}", "{tag_q-text-answer_301}", "{tag_q-text-answer_302}", "{tag_q-text-answer_303}", "{tag_q-text-answer_304}", "{tag_q-multichoice-answer_3}{tag_q-t/f-answer_3}", "{tag_q-visual-reference_3}", "{tag_q-youtube_3}{tag_q-vimeo_3}{tag_q-image_3_value}", "{tag_q-next-question_3}", "CAT_Custom_30", "CAT_Custom_31", "CAT_Custom_33", "CAT_Custom_34", "CAT_Custom_35", "CAT_Custom_36", "CAT_Custom_32", "CAT_Custom_37", "CAT_Custom_38", "CAT_Custom_39", "CAT_Custom_40");

    for (var i = 1; i <= 15; i++) {
        if (question[i].prototype.nextQ === "Yes") {
            question[i].createQuestion();
        } else {
            question1.createQuestion();
        };
    }   

</script>

【问题讨论】:

  • 我认为你不能这样做:问题[i]
  • 代码认为你正在尝试使用一个名为 question 的数组。
  • 这甚至不是有效的代码。你确定你都贴了? question 应该是什么变量?
  • @MiniRagnarok 谢天谢地你提醒了我。但是,我指的是他从不关闭顶部的功能。显然问题变量是一个问题。
  • @MiniRagnarok 果然。恭喜你。

标签: javascript oop object dynamic for-loop


【解决方案1】:

您已经创建了 3 个单独的对象,并且您试图将它们作为一个数组来访问,但是它们是个人而不是数组。

试试这个

var question = new Array();

question[1] = new Question("1", "{tag_q-question_1}", "{tag_q-answer-type_1}", "{tag_q-text-answer_101}", "{tag_q-text-answer_102}", "{tag_q-text-answer_103}", "{tag_q-text-answer_104}", "{tag_q-multichoice-answer_1}{tag_q-t/f-answer_1}", "{tag_q-visual-reference_1}", "{tag_q-youtube_1}{tag_q-vimeo_1}{tag_q-image_1_value}", "{tag_q-next-question_1}", "CAT_Custom_13", "CAT_Custom_11", "CAT_Custom_14", "CAT_Custom_15", "CAT_Custom_16", "CAT_Custom_17", "CAT_Custom_7", "CAT_Custom_8", "CAT_Custom_9", "CAT_Custom_10", "CAT_Custom_12");
question[2] = new Question("2", "{tag_q-question_2}", "{tag_q-answer-type_2}", "{tag_q-text-answer_201}", "{tag_q-text-answer_202}", "{tag_q-text-answer_203}", "{tag_q-text-answer_204}", "{tag_q-multichoice-answer_2}{tag_q-t/f-answer_2}", "{tag_q-visual-reference_2}", "{tag_q-youtube_2}{tag_q-vimeo_2}{tag_q-image_2_value}", "{tag_q-next-question_2}", "CAT_Custom_19", "CAT_Custom_20", "CAT_Custom_22", "CAT_Custom_23", "CAT_Custom_24", "CAT_Custom_25", "CAT_Custom_21", "CAT_Custom_26", "CAT_Custom_27", "CAT_Custom_28", "CAT_Custom_29");
question[3] = new Question("3", "{tag_q-question_3}", "{tag_q-answer-type_3}", "{tag_q-text-answer_301}", "{tag_q-text-answer_302}", "{tag_q-text-answer_303}", "{tag_q-text-answer_304}", "{tag_q-multichoice-answer_3}{tag_q-t/f-answer_3}", "{tag_q-visual-reference_3}", "{tag_q-youtube_3}{tag_q-vimeo_3}{tag_q-image_3_value}", "{tag_q-next-question_3}", "CAT_Custom_30", "CAT_Custom_31", "CAT_Custom_33", "CAT_Custom_34", "CAT_Custom_35", "CAT_Custom_36", "CAT_Custom_32", "CAT_Custom_37", "CAT_Custom_38", "CAT_Custom_39", "CAT_Custom_40");

for (var i = 1; i <= 3; i++) {
    if (question[i].nextQ === "Yes") {
        question[i].createQuestion();
    } else {
        question[1].createQuestion();
    };
}   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 2018-09-03
    • 2021-05-08
    • 1970-01-01
    • 2012-09-12
    相关资源
    最近更新 更多