简体   繁体   English

如何突出显示在线测验的正确和错误答案?

[英]How can I highlight correct and incorrect answers to an online quiz?

I am creating a simple quiz about country capitals and would like to have the website highlight the correct answers (in green) for each question after the user hits the submit button as well as highlight any incorrect answers chosen (in red). 我正在创建一个有关国家首都的简单测验,并希望在用户单击“提交”按钮后,网站突出显示每个问题的正确答案(绿色),并突出显示选择的任何错误答案(红色)。 I have searched on this site under similar questions, but the answers provided confused me. 我在这个站点上搜索了类似的问题,但是提供的答案使我感到困惑。

I would like to have the background colors activated along with the alert message that gives the percentage of correct answers. 我想将背景色与警告消息一起激活,以给出正确答案的百分比。 Currently, the alert works perfectly fine, but I would like assistance for the background colors. 目前,警报效果很好,但是我想为背景色提供帮助。

 function check() { var q1 = document.quiz.q1.value; var q2 = document.quiz.q2.value; var q3 = document.quiz.q3.value; var q4 = document.quiz.q4.value; var q5 = document.quiz.q5.value; var count = 0; if (q1 == "c") { count++; } if (q2 == "d") { count++; } if (q3 == "c") { count++; } if (q4 == "b") { count++; } if (q5 == "d") { count++; } var final = (count / 5) * 100 alert("Your got " + final + "% correct!"); } 
 body, html { height: 100%; width: 100%; margin: 0; color: rgb(0, 0, 0); font-size: 100%; font-weight: bold; } body { text-align: left; padding-left: 25px; background-color: rgb(255, 253, 208); } h1 { color: rgb(0, 0, 0); } .Correct { background-color: lawngreen; } .Incorrect { background-color: red; } 
 <form id="quiz" name="quiz"> <p>What is the capital of the USA?</p> <br> <input type="radio" name="q1" value="a" class="Incorrect">Chicago <br> <input type="radio" name="q1" value="b" class="Incorrect">Maimi<br> <input type="radio" name="q1" value="c" class="Correct">Washington DC <br> <input type="radio" name="q1" value="d" class="Incorrect">Vegas<br> <br> <p>What is the current capital of Brazil?</p> <br> <input type="radio" name="q2" value="a" class="Incorrect">Salvador <br> <input type="radio" name="q2" value="b" class="Incorrect">Rio de Janero <br> <input type="radio" name="q2" value="c" class="Incorrect">Sao Paulo <br> <input type="radio" name="q2" value="d" class="Correct">Brasilia <br> <br> <p>What is the capital of Japan?</p> <br> <input type="radio" name="q3" value="a" class="Incorrect">Osaka <br> <input type="radio" name="q3" value="b" class="Incorrect">Kyoto <br> <input type="radio" name="q3" value="c" class="Correct">Tokyo<br> <input type="radio" name="q3" value="d" class="Incorrect">Sapporo<br> <br> <p>What is the capital of Germany?</p> <input type="radio" name="q4" value="a" class="Incorrect">Munich<br> <input type="radio" name="q4" value="b" class="Correct">Berlin<br> <input type="radio" name="q4" value="c" class="Incorrect">Cologne<br> <input type="radio" name="q4" value="d" class="Incorrect">Hamburg<br> <p>What is the capital of South Korea? </p> <input type="radio" name="q5" value="a" class="Incorrect">Busan<br> <input type="radio" name="q5" value="b" class="Incorrect">Incheon<br> <input type="radio" name="q5" value="c" class="Incorrect">Jeju<br> <input type="radio" name="q5" value="d" class="Correct">Seoul<br> <br/> <input type="button" value="submit" onclick="check()"> </form> 

You're currently trying to style the input radio button (the clickable circle), but are expecting the text to be styled instead. 您当前正在尝试设置输入单选按钮(可单击的圆圈)的样式,但是希望改成文本的样式。 Traditionally, you should have the text and input for each choice wrapped in a <label> tag. 传统上,您应该将每个选项的文本和输入都包装在<label>标记中。 Then apply the css style to each label. 然后将css样式应用于每个标签。 I did the first one for you in your code below. 我在下面的代码中为您做了第一个。

Also, add for="..." to each label, so that when the text is clicked, that corresponding input box is also selected. 另外,在每个标签上添加for="..." ,以便在单击文本时也选择相应的输入框。 You will also need to add an id="..." to each input for this to work. 您还需要在每个输入中添加一个id="..." ,此功能才能起作用。

 // js truncated, no changes 
 .Incorrect { background-color: red; } // css truncated, no changes 
 <form id="quiz" name="quiz"> <p>What is the capital of the USA?</p> <br> <label for="q1a" class="Incorrect"> <input type="radio" name="q1" id="q1a" value="a"> Chicago </label> <br> <input type="radio" name="q1" value="b" class="Incorrect">Maimi<br> <input type="radio" name="q1" value="c" class="Correct">Washington DC <br> <input type="radio" name="q1" value="d" class="Incorrect">Vegas<br> <br> <input type="button" value="submit" onclick="check()"> </form> 

Additionally to chadiusvt Answer you can add some Lines like this: 除chadiusvt Answer之外,您还可以添加以下几行:

This at the End of CSS Area(just next to your .Incorrect part): 这在CSS区域的末尾(就在.Incorrect部分的旁边):

.nocolor {
    background-color: rgb(255, 253, 208);
}

This at the End of JS Area(after Alert, but inside of the function): 这在JS区域的末尾(在Alert之后,但在函数内部):

var elem = document.getElementsByClassName("nocolor");
while (elem.length)
    elem[0].className = elem[0].className.replace(/\bnocolor\b/g, "");

Now add the class 'nocolor' to all Label-Elements. 现在,将“ nocolor”类添加到所有Label-Elements中。 Looks like this eg 看起来像这样

<label class="Incorrect nocolor" ><input type="radio" name="q1" value="a">Chicago</label><br>
<label class="Incorrect nocolor" ><input type="radio" name="q1" value="b">Maimi</label><br>
<label class="Correct   nocolor" ><input type="radio" name="q1" value="c">Washington D.C.</label><br>

This new class 'nocolor' will hide the backgroundcolors until you click the Button. 这个新的类“ nocolor”将隐藏backgroundcolors,直到您单击Button。 This need to be after .Correct and .Incorrect so it will overwrite the red/green Background temporary! 必须在.Correct和.Incorrect之后,以便它将覆盖红色/绿色的Background临时!

The three Lines of JS: 1. Line looks for all Elements with class 'nocolor' 2. Line goes through the Elements and 3. Line removes all of the nocolor-classes on each Element. JS的三行:1.行查找所有带有'nocolor'类的元素。2.行遍历元素,并且3.行删除了每个Element上的所有nocolor类。 = the red/green Backgrounds will be visible again(after clicking the Button). =红色/绿色背景将再次可见(单击按钮后)。

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

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