简体   繁体   English

如何在 MCQ 测验中突出显示不同问题的正确答案?

[英]How to highlight correct answers for different questions in a MCQ quiz?

I am trying to develop a simple Javascript function which highlights the chosen answer to red if the answer is incorrect, and green if correct.我正在尝试开发一个简单的 Javascript function 如果答案不正确,则将所选答案突出显示为红色,如果正确则将其突出显示为绿色。 If another incorrect answer is chosen, the previous incorrect answer must switch to neutral background and the new incorrect answer must be in red.如果选择了另一个错误答案,则先前的错误答案必须切换到中性背景,并且新的错误答案必须为红色。 Also we must have the same functionality for multiple questions - the red and green for previous questions must remain as we move on to new questions.此外,我们必须对多个问题具有相同的功能——在我们继续处理新问题时,之前问题的红色和绿色必须保持不变。

I have an example here , but can someone help me add the above functionality?我在这里有一个例子,但是有人可以帮我添加上述功能吗?

HTML: HTML:

1. A sum of money is to be distributed among P, Q, R, and S in the proportion 5:2:4:3 respectively. If R gets ₹1000 more than S, what is the share of Q (in ₹)?

<div id="item" > </div>
<button id="incorrect" class="btn" name="q1" onClick="click_action();"> 500</button>
<button id="incorrect" class="btn" name="q1" onClick="click_action();"> 1000</button>
<button id="incorrect" class="btn"  name="q1" onClick="click_action();"> 1500</button>
<button id="correct" class="btn" name="q1" onClick="click_action();"> 2000</button>

<br><br>

2. A trapezium has vertices marked as P, Q, R and S (in that order anticlockwise). The side PQ is parallel to side SR. Further, it is given that, PQ = 11 cm, QR = 4 cm, RS = 6 cm and SP = 3 cm. What is the shortest distance between PQ and SR (in cm)?
<br>
<div id="answer-buttons" class="btn-grid">
  <button id="correct" class="btn" name="q2" onClick="click_action();"> 1.80 </button>
  <button id="incorrect" class="btn" name="q2" onClick="click_action();"> 2.40 </button>
  <button  class="btn" id="incorrect" name="q2" onClick="click_action();"> 4.20 </button>
  <button class="btn" id="incorrect" name="q2" onClick="click_action();"> 5.76 </button>
  </div>

CSS CSS

.btn {
  --hue: var(--hue-neutral);
  border: 10px solid hsl(var(--hue), 100%, 30%);
  background-color: blue;
  border-radius: 100px;
  padding: 10px 30px;
  color: white;
  outline: none;
}
.btn-grid {
  display: grid;
  grid-template-columns: repeat(2, auto);
  gap: 10px;
  margin: 20px 0;
}

JS JS

function click_action() {
var incorrect = document.getElementById("incorrect");
var correct = document.getElementById("correct");


if (incorrect.style.background != "red") {
   incorrect.style.background = "red";
}
else if (correct.style.background != "green") {
   correct.style.background = "green";
}

}

Though this not not the best way to do but you can achive something similar like this.虽然这不是最好的方法,但你可以达到类似的效果。 And I will recommend you to view some articles regarding how to create quiz in javascript:我建议您查看一些有关如何在 javascript 中创建测验的文章:

 let incorrectarr = []; let correctAnswer = []; //getting all buttons let buttons = document.querySelectorAll('.btn'); //looping through each button and implement the function buttons.forEach(button => { button.addEventListener('click', function() { incorrectarr.push(button) checkFunction(button); showAnswer(); incorrectarr.forEach(e=> e.style.background = 'red') }); }); function checkFunction(a) { //looping through button and keep the background color blue buttons.forEach(button => { button.style.background = "blue"; }); //if you console.log(id) you will get the id and according to the id change its style if(a.id === "correct"){ a.style.background = "green" } if(a.id === "incorrect"){ a.style.background = "red" } } let item = document.querySelectorAll('.item'); function showAnswer(){ item.forEach(i=>{ // console.log(i.children['correct']) i.addEventListener('click',function(){ let correct = i.children['correct']; correctAnswer.push(correct); correctAnswer.forEach(e=>{ e.style.background = 'green' }) }) }) }
 .btn { --hue: var(--hue-neutral); border: 10px solid hsl(var(--hue), 100%, 30%); background-color: blue; border-radius: 100px; padding: 10px 30px; color: white; outline: none; }.btn-grid { display: grid; grid-template-columns: repeat(2, auto); gap: 10px; margin: 20px 0; }
 <body> <div class="questions"> <p> 1. A sum of money is to be distributed among P, Q, R, and S in the proportion 5:2:4:3 respectively. If R gets ₹1000 more than S, what is the share of Q (in ₹)?</p> <div class="item"> <button id="incorrect" class="btn"> 500</button> <button id="incorrect" class="btn"> 1000</button> <button id="incorrect" class="btn"> 1500</button> <button id="correct" class="btn"> 2000</button> </div> </div> <div class="questions" > <p>2. A trapezium has vertices marked as P, Q, R and S (in that order anticlockwise). The side PQ is parallel to side SR. Further, it is given that, PQ = 11 cm, QR = 4 cm, RS = 6 cm and SP = 3 cm. What is the shortest distance between PQ and SR (in cm)? </p> <div class="item"> <button id="correct" class="btn"> 1.80 </button> <button id="incorrect" class="btn"> 2.40 </button> <button class="btn" id="incorrect"> 4.20 </button> <button class="btn" id="incorrect"> 5.76 </button> </div> </div> <script src="src/index.js"> </script> </body>

If you haven't understand any single line of it let me know.如果您还没有理解其中的任何一行,请告诉我。

id attribute must be unique in html. id 属性在 html 中必须是唯一的。 You have the same id to more than one button.您对多个按钮具有相同的 id。 You can make a check like data-correct="1" and data-correct="0" .您可以进行检查,例如data-correct="1"data-correct="0" Then you should move the event object to your javascript function and change the background color according to the equivalence of event.target.getAttribute("data-correct") to equals "1"然后您应该将事件 object 移动到您的 javascript function 并根据event.target.getAttribute("data-correct")的等效性更改背景颜色为 equals

update your html:更新您的 html:

<button class="btn" data-correct="0" name="q1"> 500</button>
<button class="btn" data-correct="0" name="q1"> 1000</button>
<button class="btn" data-correct="1" name="q1"> 1500</button>
<button class="btn" data-correct="0" name="q1"> 2000</button>

and init click listeners:并初始化点击监听器:

function init() {
    const buttons = [...document.getElementsByClassName("btn")];
    buttons.forEach(button => {
        button.addEventListener("click", click_action)
    })
}
init()

and click_action function和 click_action function

function click_action(e) {
    const correctAttribute = e.target.getAttribute("data-correct");
    if(Boolean(+correctAttribute)) {
        e.target.style.background = "green";
    }else {
        e.target.style.background = "red";
    }

} }

now green when clicking on correct answers and red when clicking on wrong answers现在点击正确答案时为绿色,点击错误答案时为红色

暂无
暂无

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

相关问题 如何突出显示在线测验的正确和错误答案? - How can I highlight correct and incorrect answers to an online quiz? 如何在javascript中将提交的测验答案数组与正确答案数组进行比较 - How to compare an array of submitted quiz answers with an array of correct answers in javascript 我正在使用 MCQ 创建一个网站。 我如何显示一个人选择的正确答案的数量? - I am creating a website with MCQ. How I can show the number of correct answers a person selected? 我有一个有效的 jQuery 测验,但是一旦做出正确答案,它就会隐藏对先前问题的错误答案 - I have a jQuery quiz that works but it hides the wrong answers to the previous questions once an a correct answer is made 允许单独的正确答案,测验 - Allowing separate correct answers, Quiz 我如何在选择时显示正确和错误的答案以及在我的 mcq 网站上选择的人的正确答案数量? - How I can show the correct and incorrect answer at the moment of selection and the number of correct answers a person selected in my mcq website? 如何使用 JavaScript 验证几个 mcq 问题? - How to validate several mcq questions using JavaScript? 在线测验:通过选择不同的单选按钮,将答案和问题分为3个类别 - Online-Quiz: Dividing the answers and questions in to 3 Categories by choosing different Radio-Buttons 如何在此测验应用程序中只显示一次正确答案和错误答案? - How can I make the correct answers appear with the incorrect answers only once in this quiz app? 的JavaScript。 如何返回问题并更正Math.random的答案 - Javascript. How to return the questions and correct answers to a Math.random
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM