[英]How to disable a group of radio buttons if one has been selected and display an alert if one is not selected and the next button is clicked
I am trying to disable the rest of the radio buttons if one has been selected for a multiple-choice quiz.如果已为多项选择测验选择了一个,我正在尝试禁用单选按钮的 rest。 I tried using a forEach loop but it is skipping the forEach loop and just triggering the alert box in the else part of the if statement when I click a radio button before one is even selected.
我尝试使用 forEach 循环,但它跳过了 forEach 循环,当我在一个单选按钮被选中之前单击单选按钮时,它只是触发了 if 语句的 else 部分中的警报框。 Here is the function I am working in. the
name${questionNumber}
is the way to call my radio buttons.这是我正在使用的 function。
name${questionNumber}
是调用我的单选按钮的方式。 The myQuestions is my object with all of the questions and answers stored in it. myQuestions 是我的 object,其中存储了所有问题和答案。 Can someone please help me figure out how to do this, I would appreciate it.
有人可以帮我弄清楚如何做到这一点,我将不胜感激。 Thanks.
谢谢。
function showExplanation(questionNumber) { document.getElementsByClassName("explanations")[questionNumber].classList.add("show"); if (document.getElementsByName(`name${questionNumber}`).checked) { myQuestions.forEach((questionNumber) => { document.getElementsByName(`name${questionNumber}`).disabled = true; }); } else { alert("Please select an answer."); } } const myQuestions = [{ question: "What color is the sky?", answers: { a: "blue", b: "green", c: "red", d: "yellow" }, explanation: { correct: "Correct answer: A. blue", explain: `The sky may be blue today `, source: "Source: common sense" }, correctAnswer: "a" }, { question: `What color is an elephant?`, answers: { a: "blue", b: "grey", c: "black", d: "green" }, explanation: { correct: "Correct answer: B. grey", explain: `Most elephants are grey `, source: "Source: life experience" }, correctAnswer: "b" } ];
<!-- START OF QUIZ--> <div id="border" class="uk-align-center uk-width-1-2"> <div class="quiz-body"> <h1 class="h1-quiz">Test Your Knowledge</h1> <div class="quiz-container"> <div id="quiz"></div> </div> <div id="results" class="uk-text-center uk-text-large uk-text-bold uk-margin-small-top"></div> </div>
What is your HTML code look like??你的 HTML 代码是什么样的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.