gpt4 book ai didi

javascript - 如何使用 JavaScript 验证多个 mcq 问题?

转载 作者:行者123 更新时间:2023-11-28 09:59:07 24 4
gpt4 key购买 nike

我真的需要立即使用 Javascript。

我在 xhtml 中创建了一个多项选择题测验,并尝试使用 Javascript 进行验证。问题是,如果有任何问题未得到解答,我可以显示警报消息,但我想在同一警报消息中列出未解答的问题编号。请参阅下面的代码。

<script type="text/javascript">
<!--
function validateRadio(radio) {
var retval = false
for ( var i = 0; i < radio.length; i++ ) {
if ( radio[i].checked ) retval = true;
}
return retval;
}

// to make sure all questions are answered
function validateForm(theForm) // to make sure all questions are answered
{
var retval = true;
if ( !validateRadio(theForm.q1) ) retval = false;
if ( !validateRadio(theForm.q2) ) retval = false;
if ( !validateRadio(theForm.q3) ) retval = false;
if ( !retval ) alert("Answer all of the questions");
return retval;
}

// --></script>

<form name="quiz" action="result.php" method="post">

<p>1. Choose the correct set of solution(s) by solving <img src="images/image002.png" width="93" height="17" alt=""/> equation?<br/>
&nbsp;&nbsp;&nbsp;<input type="radio" name="q1" value="1"/>gh<br/>
&nbsp;&nbsp;&nbsp;<input type="radio" name="q1" value="2"/>hk<br/>
&nbsp;&nbsp;&nbsp;<input type="radio" name="q1" value="3"/>h<br/>
&nbsp;&nbsp;&nbsp;<input type="radio" name="q1" value="4"/>ncb</p>

<p>2. What is the value of <i>x</i> when 78x ?<br/>

&nbsp;&nbsp;&nbsp;<input type="radio" name="q2" value="1"/>90<br/>
&nbsp;&nbsp;&nbsp;<input type="radio" name="q2" value="2"/>656<br/>
&nbsp;&nbsp;&nbsp;<input type="radio" name="q2" value="3"/>2<br/>
&nbsp;&nbsp;&nbsp;<input type="radio" name="q2" value="4"/>5</p>

<p>3. What is the value of 5x ?<br/>
<input type="radio" name="q3" value="1"/>+3<br/>
<input type="radio" name="q3" value="2"/>+6<br/>
<input type="radio" name="q3" value="3"/>-3<br/>
<input type="radio" name="q3" value="4"/>-6</p>


<p><input type="submit" name="submit" value="Submit Now!" onclick="return validateForm(this.form)"/></p>

最佳答案

这样做:

function validateForm(theForm) // to make sure all questions are answered
{
var retval = true;
var unanswered = "";
if ( !validateRadio(theForm.q1) ){retval = false;unanswered += "Question1\n";}
if ( !validateRadio(theForm.q2) ){retval = false;unanswered += "Question2\n";}
if ( !validateRadio(theForm.q3) ){retval = false;unanswered += "Question3\n";}
if ( !retval ) alert("Answer all of the questions\nThe following were unanswered:\n" + unanswered);
return retval;
}

关于javascript - 如何使用 JavaScript 验证多个 mcq 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9492250/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com