【发布时间】:2014-07-01 08:42:53
【问题描述】:
我目前正在苦苦挣扎——我是 javascript 新手,在编写小脚本时遇到了麻烦。我正在尝试在 Business Catalyst 中创建一个测验。
我已将每个问题设置为一个对象,并创建一个数组,并在页面加载时分配每个问题的动态属性。
在同一页面上,我有一个提交表单,其中记录了用户的答案以及问题的文本和值。我正在努力解决的问题是我有四种不同的测验/调查答案类型。我想在页面加载时在下拉列表中标记正确的答案类型。我已经尝试了大约 15 种不同的脚本来获取要记录的所选值,但无法找到一种明确的方法来标记它。我不断收到各种错误,让我相信这是因为我试图从对象内运行脚本。
以下是我的代码,请注意,您看到的任何 {tag} 项都是插入动态文本的业务催化剂方式。此外,我正在努力解决的特定脚本位于对象的末尾(this.getAnswerType)。我在页面上还有其他脚本,但为了节省空间,我将它们排除在外。
如果有人能帮助我了解如何让正确的脚本工作以及它为什么工作,我将不胜感激。谢谢!
var quizSurvey = "{tag_quiz/survey}";
function Question(questionNumber, questionText, answerType, answerA, answerB, answerC, answerD, correctAnswer, visualRef, refKey, nextQ, qTextField, aTypeField, mcAField, mcBField, mcCField, mcDField, mcUserAnswerField, tfUserAnswerField, sRatings, sSAnswer, passFail, sAnswerType) {
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.mcUserAnswerField = mcUserAnswerField;
this.tfUserAnswerField = tfUserAnswerField;
this.sRatings = sRatings;
this.sSAnswer = sSAnswer;
this.passFail = passFail;
this.sAnswerType = sAnswerType;
this.answer = "";
this.vimeo = "";
this.youtube = "";
this.img = "";
this.layoutVimeo = "";
this.visual = "";
this.getAnswerType = function() {
dropdown = document.getElementById(this.aTypeField);
valueToSet = this.answerType;
for (var i = 0; i < dropdown.options.length; i++) {
if (dropdown.options[i].text == valueToSet) {
dropdown.options[i].selected = true;
return;
}
}
};
};
var question = new Array();
question[0] = 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", "{tag_s-answer-type_1}");
question[1] = 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", "{tag_s-answer-type_2}");
我在这里调用脚本:
if (quizSurvey == "Quiz") {
for (var i = 0; i <= 14; i++) {
if (question[i].questionText != "") {
question[i].visRefType();
question[i].createQuestion();
allQuestions = allQuestions + 1;
question[i].getAnswerType();
} else {
};
};
};
【问题讨论】:
标签: javascript oop select dynamic drop-down-menu