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

如何使用Javascript标记从对象中选择的下拉列表,javascript,oop,select,dynamic,drop-down-menu,Javascript,Oop,Select,Dynamic,Drop Down Menu,我现在很挣扎——我是javascript新手,写小脚本有困难。我正在尝试在Business Catalyst中创建一个测验 我已经将每个问题设置为一个对象,并创建一个数组,在页面加载时指定每个问题的动态属性 在同一页上,我有一个提交表单,记录用户的答案以及问题的文本和值。我正在努力解决的问题是,我有四种不同的问答/调查答案类型。我想在页面加载时在下拉列表中标记正确的答案类型。我尝试了大约15种不同的脚本来记录所选的值,但无法找到一种确定的方法来标记它。我不断地遇到各种错误,使我相信这是因为我试图

我现在很挣扎——我是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}");
var quizSurvey=“{tag_quick/survey}”;
功能问题(问题编号、问题文本、答案类型、答案A、答案B、答案C、答案D、正确答案、可视参考、参考键、下一个TQ、qTextField、aTypeField、McField、mcBField、McField、McField、mcUserAnswerField、tfUserAnswerField、sRatings、sSAnswer、passFail、sAnswerType){
this.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.vimeo=“”;
this.youtube=“”;
this.img=“”;
这个。layoutVimeo=“”;
此参数为“=”;
this.getAnswerType=函数(){
dropdown=document.getElementById(this.aTypeField);
valueToSet=this.answerType;
对于(变量i=0;i
我把脚本称为:

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 {

        };
    };
};
if(quizSurvey==“测验”){

对于(var i=0;i您可能需要检查
选项的
属性,以确定要选择哪个选项。我发现浏览器在这方面有点不稳定。通常我会设置选项的
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;
    }
}
for(变量i=0;i
不幸的是,这不起作用。我收到了错误'Uncaught TypeError:Cannot read property'options'null',这就是问题所在。:)调用
document.getElementById
返回了一个空值。
dropdown=document.getElementById(this.aTypeField);
正在返回一个
null
值。在代码运行时,
this.aTypeField
引用的HTML标记Id在页面上似乎不存在。