简体   繁体   English

如何使用输入收音机正确验证表单?

[英]How do I properly validate the form using input radios?

I have a problem with validating the form in function validate() method. 我在验证函数validate()方法中的表单时遇到问题。 This line of code: 这行代码:

if(radios[i].value == "yes" && radios[i].checked == true) //DEBUG INFO: skips this step to else.

is being skipped because one or both of the conditions are false, but I'm not sure which one and as well as if the condition is proper to execute. 因为其中一个或两个条件为假,所以被跳过,但是我不确定哪个条件以及该条件是否适合执行。 I was thinking that radios[i].value == "yes" will correspond to the value attribute of that input radio button (In other words, the correct answer regarding that question). 我以为radios [i] .value ==“ yes”将对应于该输入单选按钮的value属性(换句话说,关于该问题的正确答案)。

When the submit button is clicked, I simply want javascript to tell me whether it's correct or not and to check if the radio button is checked. 单击提交按钮后,我只希望javascript告诉我它是否正确,并检查单选按钮是否已选中。

Problem: I checked in the radio button, when submit button is clicked the alert for Please make sure you answer every question pops up 3 times and after that displays that I have the correct answer. 问题:我签入了单选按钮,单击“提交”按钮时,“请确保您回答每个问题”的警报弹出3次,然后显示我有正确答案。

Here's the full code: 这是完整的代码:

JavaScript: JavaScript:

// called when "Take Quiz" button is clicked
function takeQuiz()
{
// hide the intro
document.getElementById('intro').style.display = 'none';

// display the quiz
document.getElementById('message').style.overflow = 'auto';
document.getElementById('quiz').style.visibility = 'visible';
document.getElementById('gl_banner').style.display = 'block';
document.getElementById('gl_banner').style.visibility = 'visible';
}

//document.getElementById('submit').onclick = validateQuiz; //calls the function "validateQuiz" when submit button is clicked
// check for validation in the quiz
function validateQuiz()
{
var radios; // access elements by object name (DOM)
var i; // int variable
var right; // boolean variable to determine correct answer

radios = document.getElementById('question1').getElementsByTagName('input');
/*radios = document.getElementById('question2').getElementsByTagName('input');
radios = document.getElementById('question3').getElementsByTagName('input');
radios = document.getElementById('question4').getElementsByTagName('input');
radios = document.getElementById('question5').getElementsByTagName('input');*/

right = true;

// loop to check each radio button for validation
for(i = 0; i < radios.length; i++)
{
    if(radios[i].value == "yes" && radios[i].checked == true) //DEBUG INFO: skips this step to else.
    {
        right = true;
    }
    else if(radios[i].checked == false)
    {
        right = false;
        alert("Please check to make sure you have answered every question.");

    }
}

if(right)
{
    alert("You have answered correctly!");
}
else
{
    alert("Wrong answer");
}
}

HTML Code: HTML代码:

<div id="message" style="overflow:hidden;"><div id="intro">Why not go ahead and take the quiz to test your knowledge based on what you've learned in Smartphone Photography.
            There are only 5 questions surrounding the content of this site.
            <br/>
            <button id="takeQuiz" type="button" name="name" onclick="takeQuiz()" style="cursor:pointer;">Take Quiz!</button></div>
            <div id="gl_banner" style="display:none; visibility:hidden;">Good Luck! :)</div>
                <form id="quiz" action="#" method="post" style="visibility:hidden;" autocomplete="off">
                    <!--QUIZ-->
                    <h3>1. How many percent of modern camera phones use CMOS?</h3>
                    <div id="question1">
                        <input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
                        <label for="question-1-answers-A">A) 20%</label>
                        <br/>
                        <input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
                        <label for="question-1-answers-B">B) 80%</label>
                        <br/>
                        <input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
                        <label for="question-1-answers-C">C) 50%</label>
                        <br/>
                        <input type="radio" name="question-1-answers" id="question-1-answers-D" value="yes" />
                        <label for="question-1-answers-D">D) 90%</label>
                    </div>

**Edited for a pure javascript solution. **针对纯JavaScript解决方案进行了编辑。

I got the function to get the select value from this post . 我得到了从这篇文章中获取选择值的功能。

I don't think you need to do a loop here, as you only actually need to check one value- the value of the checked radio. 我认为您不需要在此处进行循环,因为您实际上只需要检查一个值-被检查无线电的值即可。

At the moment your looping through all the radios, so you'll always get three wrong answers. 此刻,您在所有收音机中循环播放,因此您总是会得到三个错误的答案。

**Edited again to fix some code errors. **再次编辑以修复一些代码错误。 I have tested the following, it is working for me. 我已经测试了以下内容,它对我有用。

function getRadioValue(name) {
var group = document.getElementsByName(name);

for (var i=0;i<group.length;i++) {
    if (group[i].checked) {
    return group[i].value;
    }
}

return '';
}

document.getElementById('submit').onclick = validateQuiz; //calls the function         "validateQuiz" when submit button is clicked

// check for validation in the quiz
function validateQuiz(){

right = true;
radio = getRadioValue("question-1-answers");

if(!radio.length) {
    right = false;
    alert("Please check to make sure you have answered every question.");
    return;
    }
if(radio == 'yes')
   {
    alert("You have answered correctly!");
}
else {
    right = false;
    alert("Wrong answer");
    }
}

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

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