简体   繁体   English

你如何用javascript编写多项选择?

[英]How do you code a multiple choice with javascript?

I have a multple choice exam.我有一个选择题考试。 Now, I'm thinking of using the if else conditon.现在,我正在考虑使用 if else 条件。 example below下面的例子

<li>Gabriel belongs to an <u>affluent</u> family.</li>
    <ol type="A">
        <li id="x">A</li>
        <li id="y">B</li>
        <li id="y">C</li>
    </ol>
</li>

JS code JS代码

var x = document.getElementById("x");
if (x.checked){
    document.getElementById('q2').style.display = 'block'; /*this is a next question*/
} else {
    document.getElementById('x').style.backgroundColor = "green";
} 

I would like to know, if there are alternative ways to execute this code in a simpler/easy way.我想知道,是否有其他方法可以以更简单/简单的方式执行此代码。 Because this is a 50item exam per subject.因为这是每个科目的 50 项考试。

The process of the code is,代码的过程是,

  • when the user chooses an answer and click the next button, a dialog box will show asking if the user is final with his answer.当用户选择一个答案并单击下一步按钮时,将显示一个对话框,询问用户是否对他的答案做出了最终决定。
  • if the user chooses the correct answer and clicks next, the next question will show如果用户选择正确答案并单击下一步,则将显示下一个问题
  • if the user chooses the wrong answer, the correct answer will show by highlighting or coloring the answer green.如果用户选择了错误的答案,正确的答案将通过突出显示或将答案涂成绿色来显示。

Thanks in advance...提前致谢...

I know that the code above is not right, but I just show it for an example.我知道上面的代码不对,但我只是举个例子。

No, there aren't many ways to optimize this.不,没有很多方法可以优化这一点。 About all you can optimize is how these events are attached to the answer options.关于所有您可以优化的是这些事件如何附加到答案选项。 For example, placing the same class attribute on all <ol> tags, and then applying the event handler to all of the child elements of each element with that specific class.例如,在所有<ol>标记上放置相同的 class 属性,然后将事件处理程序应用于具有该特定 class 的每个元素的所有子元素。 This is micro-optimization, and many people view it as unnecessary.这是微优化,很多人认为没有必要。

HTML: HTML:

<div id="q2" style="display: none;">
    Is that your final answer?
</div>

<li>Gabriel belongs to an <u>affluent</u> family.</li>
    <ol type="A" class="answer-group">
        <li id="x" class="correct">A</li>
        <li id="y">B</li>
        <li id="z">C</li>
    </ol>
</li>

<li>Daniel is taken into <u>captivity</u> by Nebuchadnezzar.</li>
    <ol type="A" class="answer-group">
        <li id="x">A</li>
        <li id="y">B</li>
        <li id="z" class="correct">C</li>
    </ol>
</li>

JavaScript: JavaScript:

var answerGroups = document.getElementsByClassName ('answer-group');
var q2 = document.getElementById ('q2');
var answers;

function answerOnclick (answer)
{
    answer.onclick = function (event) {
        var correct;

        if (this.classList.contains ('correct') === true) {
            q2.style.display = 'block';
        } else {
            correct = answer.parentNode.getElementsByClassName ('correct');
            correct[0].style.backgroundColor = 'green';
        }
    };
}

for (var g = 0, gl = answerGroups.length; g < gl; g++) {
    answers = answerGroups[g].children;

    for (var a = 0, al = answers.length; a < al; a++) {
        answerOnclick (answers[a]);
    }
}

I think you could use radio buttons to build your exam an have all the answers in a JavaScript object to match with the user inputs using a single function.我认为您可以使用单选按钮来构建您的考试,并在 JavaScript 对象中包含所有答案,以使用单个函数与用户输入相匹配。

This is an example:这是一个例子:

HTML HTML

<form action="">
    <p>Choose the correct color:</p>
    <input type="radio" name="color" value="red"> Red<br>
    <input type="radio" name="color" value="green" > Green<br>
    <input type="radio" name="color" value="blue" checked=true> Blue
</form>

<div class="result"></div>

JavaScript JavaScript

var answerList = {
  color: "green",
  ...
}

var resultBox = document.querySelector('.result');

function checkAnswer(name, answers) {
  var checked = document.querySelector('input[name=' + name + ']:checked');

  return answers[name] === checked.value;
}

resultBox.innerHTML = checkAnswer('color', answerList) ? 'Correct' : 'Wrong';

Check the fiddle: https://jsfiddle.net/davegomez/4y56rzL9/检查小提琴: https : //jsfiddle.net/davegomez/4y56rzL9/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM