【问题标题】:How to mark a drop down selected from within an object with Javascript如何使用Javascript标记从对象中选择的下拉列表
【发布时间】: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


    【解决方案1】:

    您可能需要检查options 的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;
        }
    }
    

    【讨论】:

    • 不幸的是,这不起作用。我收到错误“未捕获的类型错误:无法读取 null 的属性“选项”
    • 嗯,这就是问题所在。 :) 对document.getElementById 的调用返回一个空值。
    • dropdown = document.getElementById(this.aTypeField); 的调用返回一个null 值。 this.aTypeField 引用的 HTML 标记 ID 在代码运行时似乎不存在于页面上。
    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    相关资源
    最近更新 更多