gpt4 book ai didi

javascript - 如何使用 Javascript 标记从对象内选择的下拉列表

转载 作者:行者123 更新时间:2023-11-28 08:15:34 24 4
gpt4 key购买 nike

我目前正在挣扎——我是 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 {

};
};
};

最佳答案

您可能需要检查选项value属性以确定要选择哪个选项。我发现浏览器在这方面有点不稳定。一般来说,我设置选项的 selected 属性,以及下拉列表本身的 selectedIndex :

for (var i = 0; i < dropdown.options.length; i++) {
if (dropdown.options[i].value == valueToSet) {
dropdown.options[i].selected = true;
dropdown.selectedIndex = i;
break;
}
}

关于javascript - 如何使用 Javascript 标记从对象内选择的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23635248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com