【问题标题】:How do I properly validate the form using input radios?如何使用输入无线电正确验证表单?
【发布时间】:2013-12-03 13:44:07
【问题描述】:

我在函数 validate() 方法中验证表单时遇到问题。这行代码:

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

被跳过,因为其中一个或两个条件为假,但我不确定哪个条件以及该条件是否适合执行。我在想 radios[i].value == "yes" 将对应于该输入单选按钮的 value 属性(换句话说,关于该问题的正确答案)。

当点击提交按钮时,我只想让 javascript 告诉我它是否正确并检查单选按钮是否被选中。

问题:我检查了单选按钮,当单击提交按钮时,请确保您回答每个问题都会弹出 3 次警报,然后显示我有正确的答案。

这是完整的代码:

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 代码:

<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>

【问题讨论】:

标签: javascript html css forms getelementbyid


【解决方案1】:

**为纯 javascript 解决方案编辑。

我获得了从this post 获取选择值的功能。

我认为你不需要在这里做一个循环,因为你实际上只需要检查一个值——被检查的收音机的值。

目前你正在循环播放所有的收音机,所以你总是会得到三个错误的答案。

**再次编辑以修复一些代码错误。我已经测试了以下内容,它对我有用。

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");
    }
}

【讨论】:

  • radio = $("input[type='radio']["question-1-answers"]:checked").val();缺少它所说的 ) 论点。我不知道如何使用 jquery。
猜你喜欢
  • 2010-12-19
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
  • 2014-07-24
  • 2014-06-27
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
相关资源
最近更新 更多