简体   繁体   English

使用javascript验证单选按钮测验

[英]validating radio buttons quiz with javascript

I'm trying to add validation to this small quiz I made but I can't seem to make it work correctly. 我正在尝试对我做的这个小测验添加验证,但是我似乎无法使其正常运行。 This is what I want... If none of the radio buttons are checked and user is trying to proceed to the next question then alert the user to choose an answer before proceeding to the next question. 这就是我想要的...如果未选中任何单选按钮,并且用户正尝试继续下一个问题,请在继续下一个问题之前提醒用户选择答案。 I tried adding an If statement to checkAnswer function but for some reason it only works for the first question and doesn't want to go to the next question after user has chosen an answer. 我尝试将if语句添加到checkAnswer函数,但由于某种原因,它仅适用于第一个问题,并且用户选择答案后不希望转到下一个问题。 PS I'm new to JavaScript so please be easy on me :) Codepen PS我是JavaScript的新手,所以请放轻松 :) Codepen

Here is my code. 这是我的代码。

HTML 的HTML

<h1 id="test_status"></h1>
<div id="test"></div>

JavaScript 的JavaScript

var test = document.getElementById('test');
var test_status = document.getElementById('test_status');
var pos = 0;
var correct = 0;
var question;
var choices;
var choice;
var chA, chB, chC, chD;

var questions = [
  ['What is 1+1?', '4', '7', '2', '9', 'C'],
  ['What is 1+2?', '2', '3', '4', '6', 'B'],
  ['What is 1+3?', '4', '2', '6', '7', 'A'],
  ['What is 1+4?', '9', '7', '2', '5', 'D']
];

function renderQuestion(){
  test_status.innerHTML = 'Question ' + (pos+1) + ' of ' + questions.length;

  if(pos >= questions.length){
     test_status.innerHTML = 'Test Completed';
     test.innerHTML = '<h2>You got ' + correct + ' out of ' + questions.length + ' questions correct </h2>';
    return false;
   }

  question = questions[pos][0];
  chA = questions[pos][1];
  chB = questions[pos][2];
  chC = questions[pos][3];
  chD = questions[pos][4];

  test.innerHTML = '<h2>' + question + '</h2>';
  test.innerHTML += '<input type="radio" name="choices" value="A">' + chA + '<br>';
  test.innerHTML += '<input type="radio" name="choices" value="B">' + chB + '<br>';
  test.innerHTML += '<input type="radio" name="choices" value="C">' + chC + '<br>';
  test.innerHTML += '<input type="radio" name="choices" value="D">' + chD + '<br>';
  test.innerHTML += '<button onclick="checkAnswer()"> Check Answer </button>';
}

function checkAnswer(){
  choices = document.getElementsByName('choices');
  for(var i = 0; i < choices.length; i++){
    if(choices[i].checked){
       choice = choices[i].value;
    }
  }

  if(choice === questions[pos][5]){
     correct++;  
    console.log(correct);
  }

  pos++;
  renderQuestion();
}

window.addEventListener('load', renderQuestion, false);

You can check if user has selected a choice using a flag answered like below, codepen 您可以使用下面回答的标志( codepen)检查用户是否选择了一个选项

var test = document.getElementById('test');
    var test_status = document.getElementById('test_status');
    var pos = 0;
    var correct = 0;
    var question;
    var choices;
    var choice;
    var chA, chB, chC, chD;


    var questions = [
      ['What is 1+1?', '4', '7', '2', '9', 'C'],
      ['What is 1+2?', '2', '3', '4', '6', 'B'],
      ['What is 1+3?', '4', '2', '6', '7', 'A'],
      ['What is 1+4?', '9', '7', '2', '5', 'D']
    ];

    function renderQuestion(){
      document.getElementById('error').style.display = 'none';
      test_status.innerHTML = 'Question ' + (pos+1) + ' of ' + questions.length;

      if(pos >= questions.length){
         test_status.innerHTML = 'Test Completed';
         test.innerHTML = '<h2>You got ' + correct + ' out of ' + questions.length + ' questions correct </h2>';
        return false;
       }

      question = questions[pos][0];
      chA = questions[pos][1];
      chB = questions[pos][2];
      chC = questions[pos][3];
      chD = questions[pos][4];

      test.innerHTML = '<h2>' + question + '</h2>';
      test.innerHTML += '<input type="radio" name="choices" value="A">' + chA + '<br>';
      test.innerHTML += '<input type="radio" name="choices" value="B">' + chB + '<br>';
      test.innerHTML += '<input type="radio" name="choices" value="C">' + chC + '<br>';
      test.innerHTML += '<input type="radio" name="choices" value="D">' + chD + '<br>';
      test.innerHTML += '<button onclick="checkAnswer()"> Check Answer </button>';
    }

    function checkAnswer(){
      var answered = false;
      choices = document.getElementsByName('choices');
      for(var i = 0; i < choices.length; i++){
        if(choices[i].checked){
           answered = true;
           choice = choices[i].value;
        }
      }
      if(!answered) {
        document.getElementById('error').style.display = 'block';

    }
      else {
        if(choice === questions[pos][5]){
         correct++;  
        console.log(correct);
      }

      pos++;
      renderQuestion();
      }
      }


    window.addEventListener('load', renderQuestion, false);

html --> html->

<h1 id="test_status"></h1>
<div id="test"></div>
<p id="error">Please select an ans to continue</p>

your condition only checks to see if the answer is correct, then writes to the console. 您的情况仅检查答案是否正确,然后写入控制台。 your code that advances the user to the next question runs regardless of the answer provided (or omitted). 无论提供(或省略)答案如何,都会运行将用户前进到下一个问题的代码。

you need to reset choice each time checkAnswer runs, and you need to add a condition that checks for a valid choice 您需要在每次checkAnswer运行时重置choice ,并且需要添加一个条件来检查有效choice

function checkAnswer(){
  choice = null;
  choices = document.getElementsByName('choices');
  for(var i = 0; i < choices.length; i++){
    if(choices[i].checked){
       choice = choices[i].value;
    }
  }

  if( !choice ){
    return console.error('select an answer.');
  }


  if(choice === questions[pos][5]){
     correct++;  
    console.log(correct);
  }

  pos++;
  renderQuestion();
}

you are checking only checked , you need to return; 您只checked ,需要return; if no radio button is checked. 如果未选中任何单选按钮。 updated codepen 更新的代码笔

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

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