简体   繁体   English

如果已选择一个单选按钮,如何禁用一组单选按钮,如果未选择一个单选按钮并单击下一个按钮,如何显示警报

[英]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>

You mean When Correct User Can Only Choose one answer and when User select one answer other's will be disabled??你的意思是当正确的用户只能选择一个答案时,当用户 select 一个答案时,其他人的答案将被禁用?

What is your HTML code look like??你的 HTML 代码是什么样的?

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

相关问题 如何检测是否已使用jQuery选择了组中的一个单选按钮 - How to detect if a one radio button in a group has been selected with jQuery 禁用提交按钮,直到选中一组动态创建的单选按钮中的一个 - Disable submit button until one in a group of dynamically-created radio buttons selected 检查一组单选按钮是否至少选择了一个 - Check if group of Radio-Buttons has at least one selected 检查单选按钮组是否已被选择 - Checking if radio button group has been selected 检查是否至少选择了一个单选按钮 - JavaScript - checking if at least one radio button has been selected - JavaScript 确认已在React中选择每个Div中的一个单选按钮 - Verify that One Radio Button in Every Div has been Selected In React 如何验证至少已选中这些单选按钮之一? - How do I validate that at least one of these radio buttons has been selected? 选择“单选按钮”中的一个选项时如何禁用其他选项? - How to disable other opitons when one option is selected of “radio buttons”? 启用一个无线电组的单选按钮,并在选中时禁用另一个无线电组 - To enable Radio button of one radio group and disable the other radio group when selected 禁用按钮,直到选择了AJAX生成的单选按钮 - Disable button until AJAX generated radio buttons have been selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM