javascript - 如何使用输入 radio 正确验证表单?

标签 javascript html css forms getelementbyid

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

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

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

当提交按钮被点击时,我只是想让 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 解决方案进行了编辑。

我得到了从 this post 中获取选择值的函数.

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

此刻你正在循环所有的 radio ,所以你总是会得到三个错误的答案。

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

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

关于javascript - 如何使用输入 radio 正确验证表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20081145/

相关文章:

javascript - Html 文件输入,从 File 对象中设置选择

javascript - 当观察者切换浏览器选项卡或最小化浏览器窗口时播放/暂停 Vimeo 视频

javascript - 快速 js session 中的未定义错误

javascript - Angular 4 上下文中的 "deep import"是什么?

jquery - 页面刷新后保持类切换

css - 为什么我的 CSS 表格被压到一边?

html - CSS/HTML 图像交换不起作用

javascript - slicknav 没有显示

javascript - 切换 Font Awesome 图标(如果处于事件状态)?

html - Safari 和 Chrome 媒体查询不起作用