简体   繁体   English

我如何 select 用户在我的 javascript 文件夹中选择了哪个单选标记

[英]How do I select which radio tag the user has selected in my javascript folder

I have tried the code in my showresults function to select the users answers (which would be the input tag that the user has selected as the answer to each quiz question) and comparing it to the correct answer in my questions array but whenever I do this the submit button doesn't work and I am also not sure if I should put this code in my nextquestion function or my showresults function.我已经在我的 showresults function 到 select 中尝试了用户回答的代码(这将是用户选择作为每个测验问题的答案的输入标签)并将其与我的问题数组中的正确答案进行比较但是每当我这样做时提交按钮不起作用,我也不确定是否应该将此代码放在我的下一个问题 function 或我的显示结果 function 中。

this is my script file这是我的脚本文件

const questiontext= document.getElementById('question-text');
const A= document.getElementById('OptionA');
const B= document.getElementById('OptionB');
const C= document.getElementById('OptionC');
const D= document.getElementById('OptionD');
const nextbutton= document.getElementById('next');
const resultcontainer= document.getElementById('results');
const submitbutton= document.getElementById('submit');
const options= document.querySelectorAll('options');
const questions=[
    {
        question: "What is the best item at mcdonalds?",
        answerA: "Fries",
        answerB: "Big Mac",
        answerC: "Mcnuggets",
        answerD: "Quarter Pounder",
        correctanswer: "Big Mac"
    },
    {
        question: "What is the cheapest thing on the mcdonalds menu?",
        answerA: "Fries",
        answerB: "Double Cheeseburger",
        answerC: "Happy Meal",
        answerD: "Orange juice",
        correctanswer: "Fries"
    },
    {
        question: "What is the least popular item at mcdonalds?",
        answerA: "Filet O Fish",
        answerB: "Hamburger",
        answerC: "Veggie Deluxe",
        answerD: "Mineral water",
        correctanswer: "Filet O Fish"
    },
    {
        question: "How many dips are you allowed with 20 Mcnuggets?",
        answerA: "2",
        answerB: "4",
        answerC: "3",
        answerD: "6",
        correctanswer: "4"
    }
];
//Question index at start
let questionindex= 0;
//Before they have answered any question
let numcorrect=0;
const currentquestion= () =>{
    questiontext.innerHTML= questions[questionindex].question;
    A.innerHTML= questions[questionindex].answerA;
    B.innerHTML= questions[questionindex].answerB;
    C.innerHTML= questions[questionindex].answerC;
    D.innerHTML= questions[questionindex].answerD;
}
const nextquestion= () =>{
    currentquestion();
    questionindex++
    if(questionindex === questions.length){
        submitbutton.classList.remove('hidden');
        nextbutton.classList.add('hidden'); 
     }
}
const showresults= () =>{
    if(options.checked===questions[questionindex].correctanswer){
        numcorrect++
    }
    resultcontainer.innerHTML=`You got ${numcorrect} out of ${questions.length}`;
}
//Load first question and answers
currentquestion();
//Button to display next question
nextbutton.addEventListener('click', nextquestion);
submitbutton.addEventListener('click', showresults);

This is my html file这是我的 html 文件

<!DOCTYPE html>
<html>
    <head>
        <title>Fast food facts</title>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="static/styles.css">
    </head>
    <body>
        <div id="quizcontainer">
            <div id="quiz">
                <div id="question"><h2 id="question-text"></h2></div>
                <div id="answers">
                    <label for="A" id="OptionA"></label>
                    <input type="radio" name="option" id="A" class="options">
                    
                    <label for="B" id="OptionB"></label>
                    <input type="radio" name="option" id="B" class="options">
                    
                    <label for="C" id="OptionC"></label>
                    <input type="radio" name="option" id="C" class="options">
                    
                    <label for="D" id="OptionD"></label>
                    <input type="radio" name="option" id="D" class="options">
                </div>
            </div>
        </div>
        <button id="submit" class="hidden">Submit</button>
        <button id="next">Next</button>
        <div id="results"></div>
        <script src="static/script.js"></script>
    </body>
</html>

I fixed your comparison on submit.我在提交时修复了你的比较。

First, you were not targeting the radio inputs because there was a missing dot in the selector:首先,您没有瞄准无线电输入,因为选择器中缺少一个点:

const options = document.querySelectorAll(".options");  // Added a dot to specify a class

Then, to each radio, I added a value using the currentquestion function.然后,我使用当前currentquestion为每个收音机添加了一个value

A.nextElementSibling.value = questions[questionindex].answerA;

Finally, I had all that is needed to do a comparison in showresults .最后,我拥有了在showresults中进行比较所需的一切。 A loop was necessary to go through each radio:通过每个无线电循环到 go 是必要的:

// Loop through the radio inputs to compare its value if checked
options.forEach(function(option){
  if(option.checked && option.value === questions[questionindex].correctanswer){
    console.log("Correct anser found!")
    numcorrect++;
  }
})

 const questiontext = document.getElementById("question-text"); const A = document.getElementById("OptionA"); const B = document.getElementById("OptionB"); const C = document.getElementById("OptionC"); const D = document.getElementById("OptionD"); const nextbutton = document.getElementById("next"); const resultcontainer = document.getElementById("results"); const submitbutton = document.getElementById("submit"); const options = document.querySelectorAll(".options"); // Added a dot to specify a class const questions = [ { question: "What is the best item at mcdonalds?", answerA: "Fries", answerB: "Big Mac", answerC: "Mcnuggets", answerD: "Quarter Pounder", correctanswer: "Big Mac" }, { question: "What is the cheapest thing on the mcdonalds menu?", answerA: "Fries", answerB: "Double Cheeseburger", answerC: "Happy Meal", answerD: "Orange juice", correctanswer: "Fries" }, { question: "What is the least popular item at mcdonalds?", answerA: "Filet O Fish", answerB: "Hamburger", answerC: "Veggie Deluxe", answerD: "Mineral water", correctanswer: "Filet O Fish" }, { question: "How many dips are you allowed with 20 Mcnuggets?", answerA: "2", answerB: "4", answerC: "3", answerD: "6", correctanswer: "4" } ]; //Question index at start let questionindex = 0; //Before they have answered any question let numcorrect = 0; const currentquestion = () => { questiontext.innerHTML = questions[questionindex].question; A.innerHTML = questions[questionindex].answerA; A.nextElementSibling.value = questions[questionindex].answerA; B.innerHTML = questions[questionindex].answerB; B.nextElementSibling.value = questions[questionindex].answerB; C.innerHTML = questions[questionindex].answerC; C.nextElementSibling.value = questions[questionindex].answerC; D.innerHTML = questions[questionindex].answerD; D.nextElementSibling.value = questions[questionindex].answerD; }; const nextquestion = () => { currentquestion(); questionindex++; if (questionindex === questions.length) { submitbutton.classList.remove("hidden"); nextbutton.classList.add("hidden"); } }; const showresults = () => { // Loop through the radio inputs to compare its value if checked options.forEach(function (option) { if ( option.checked && option.value === questions[questionindex].correctanswer ) { console.log("Correct anser found;"); numcorrect++; } }). resultcontainer.innerHTML = `You got ${numcorrect} out of ${questions;length}`; }; //Load first question and answers currentquestion(). //Button to display next question nextbutton,addEventListener("click"; nextquestion). submitbutton,addEventListener("click"; showresults);
 <html> <head> <title>Fast food facts</title> <meta charset="UTF-8"> <link rel="stylesheet" href="static/styles.css"> </head> <body> <div id="quizcontainer"> <div id="quiz"> <div id="question"> <h2 id="question-text"></h2> </div> <div id="answers"> <label for="A" id="OptionA"></label> <input type="radio" name="option" id="A" class="options"> <label for="B" id="OptionB"></label> <input type="radio" name="option" id="B" class="options"> <label for="C" id="OptionC"></label> <input type="radio" name="option" id="C" class="options"> <label for="D" id="OptionD"></label> <input type="radio" name="option" id="D" class="options"> </div> </div> </div> <button id="submit" class="hidden">Submit</button> <button id="next">Next</button> <div id="results"></div> <script src="static/script.js"></script> </body> </html>

暂无
暂无

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

相关问题 如何检查在javascript中选择了哪个单选按钮? - How to check which radio button has been selected in javascript? 如何让我的 Javascript 识别在我的 HTML 中选择了哪个单选按钮选项? - How can I get my Javascript to recognise which radio button option is selected in my HTML? 如何在选择第一个单选输入时选择所有复选框,并在选择第二个单选输入时取消选中所有复选框? (JavaScript)的 - How do I Select All Checkboxes When First Radio Input Selected & Uncheck All Checkboxes when Second radio input selected? (Javascript) 如何找出用户在Javascript中选择了哪个单选按钮? - How to find out which radio button the user selected in Javascript? 如何更改使用功能选择的单选按钮? - How do I change which radio button is selected using a function? 如何使用 javascript 获取所选单选按钮的 label - How do I get the label of the selected radio button using javascript 如何确定在Javascript中选择了哪个单选按钮 - How To Determine Which Radio Button Was Selected In Javascript 如何检查我的单选按钮是否已选中并且循环正常工作? - How do I check if my radio button is selected and the loop is working? 如何检查Jinja2 HTML模板中选中的单选按钮中的哪个单选按钮? - How do I check which radio button in a group of radio buttons is selected in a Jinja2 HTML Template? 当有多个具有相同名称的单选按钮时,如何检查选择了哪个单选按钮? (使用jquery) - How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM