简体   繁体   English

检查用户是否选择了正确的复选框

[英]Checking if the user selected the correct checkbox

I´m making a quiz with javascript and I have encountered a problem with getting the answer from chosen checkbox.我正在使用 javascript 进行测验,但在从所选复选框中获取答案时遇到了问题。 I have gotten the checkboxes to work so that you can only pick one answer.我已经让复选框起作用,这样你只能选择一个答案。 Reason for using checkboxes is just that I tried to use radioboxes and I didn´t get it to work that way either.使用复选框的原因只是我尝试使用单选框,但我也没有让它以这种方式工作。

Currently my form looks like this:目前我的表格是这样的:

    <div id="vastausformi">
        <form id="vastaukset"> 

            <label for="a">A)</label>
<input type="checkbox" classname="box" id="Check1" value="Value1" onclick="selectOnlyThis(this.id)" />  
    
            <label for="b">B)</label>
  <input type="checkbox" classname="box" id="Check2" value="Value1" onclick="selectOnlyThis(this.id)" />  
             
            <label for="c">C)</label>
<input type="checkbox" classname="box"  id="Check3" value="Value1" onclick="selectOnlyThis(this.id)" />   

            <label for="d">D)</label>
<input type="checkbox" classname="box" id="Check4" value="Value1" onclick="selectOnlyThis(this.id)" /> 
        </form>
    </div>

Then i have javascript for a question:然后我有一个问题的javascript:

let questions= {

  question: "Which city is the capital of Sweden?",
  vastaus:{
      a: 'Stockholm',
      b: 'Malmö',
      c: 'Göteborg',
      d: 'Helsingborg'
  },
  correctAnswer: 'a'
};

Then I have made a function that checks the users guess.然后我做了一个检查用户猜测的函数。

  function checkGuess(){
    let correctAnswer= questions.valueOf(oikeaVastaus);
    let playerChoice= document.getElementsByClassName("box");
    
    if(playerChoice===correctAnswer){
      console.log("correct");
    }

    else{
      console.log("wrong");
    }

   
  };
    
guessSubmit.addEventListener('click', checkGuess);

The checkGuess function doesn´t seem to get the "answer". checkGuess 函数似乎没有得到“答案”。 I get it so that it says that the answer is correct (no matter which checkbox you choose) or that all the answers are wrong.我明白了,所以它说答案是正确的(无论你选择哪个复选框)或者所有答案都是错误的。 I have tried to "retrieve" the information from the checkboxes with different ways with no luck.我试图用不同的方式从复选框中“检索”信息,但没有成功。 I can´t seem to get hold of that "correctAnswer".我似乎无法掌握那个“正确答案”。 I have also tried to make one specific checkbox "the correct choice", so that everytime you choose it, it would be correct, but that doesn´t work either.我还尝试将一个特定的复选框设置为“正确的选择”,这样每次您选择它时,它都是正确的,但这也不起作用。 So that checkGuess function is just one of the ways I have tried to do the checking.所以 checkGuess 函数只是我尝试进行检查的方法之一。 I have also tried to leave away the declaration of playerChoice and put the input straight to the if statement.我也试图放弃 playerChoice 的声明并将输入直接放在 if 语句中。 I have also tried to我也试过

Reason to use those consolelogs are just the see if the script works, so in the future the answer will be displayed on screen and there will be more questions than just one.使用这些控制台日志的原因只是为了查看脚本是否有效,因此将来答案将显示在屏幕上并且问题会不止一个。 I´m new to coding so I might be doing this compeletely wrong, but I wanted to make it so that one question appears and then there is four checkboxes to choose from, then there will be a button to get to next question.我是编码新手,所以我可能完全错了,但我想让它出现一个问题,然后有四个复选框可供选择,然后会有一个按钮进入下一个问题。

I apologize for my english, but hopefully you will understand what I mean.我为我的英语道歉,但希望你能理解我的意思。

If anyone has any idea how to get this to work or have some alternative ways to do this, I would greatly appreciate it!如果有人知道如何让它工作或有其他方法可以做到这一点,我将不胜感激!

I've updated the input to use radio instead of checkbox to allow users select one answer at a time我已将输入更新为使用radio而不是checkbox ,以允许用户一次选择一个答案

Also, clicking on the input will call the checkGuess method immediately with its value此外,单击输入将立即使用其值调用checkGuess方法

 let questions= { question: "Which city is the capital of Sweden?", vastaus:{ a: 'Stockholm', b: 'Malmö', c: 'Göteborg', d: 'Helsingborg' }, correctAnswer: 'a' }; function checkGuess(playerChoice){ if(playerChoice === questions.correctAnswer){ console.log("correct"); } else{ console.log("wrong"); } };
 <div id="vastausformi"> <form id="vastaukset"> <label for="a">A)</label> <input type="radio" classname="box" name="box" id="Check1" value="a" onclick="checkGuess(this.value)" /> <label for="b">B)</label> <input type="radio" classname="box" name="box" id="Check2" value="b" onclick="checkGuess(this.value)" /> <label for="c">C)</label> <input type="radio" classname="box" name="box" id="Check3" value="c" onclick="checkGuess(this.value)" /> <label for="d">D)</label> <input type="radio" classname="box" name="box" id="Check4" value="d" onclick="checkGuess(this.value)" /> </form> </div>

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

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