JavaScript单选按钮确认

JavaScript单选按钮确认,javascript,validation,loops,radio-button,confirm,Javascript,Validation,Loops,Radio Button,Confirm,我正在尝试添加一个函数,或者在现有函数中添加额外的JavaScript以进行确认。单击提交按钮后,我需要一个确认框,上面写着“您已选择(GCSE、A2或AS),是否正确?”应该有一个取消按钮返回表单,或者是一个允许提交表单的确定按钮 这是代码,我做了一些研究,它说使用一个循环,我对这个很陌生,所以我不知道该怎么做 <head> <title>Exam entry</title> <script language="javascript" type="te

我正在尝试添加一个函数,或者在现有函数中添加额外的JavaScript以进行确认。单击提交按钮后,我需要一个确认框,上面写着“您已选择(GCSE、A2或AS),是否正确?”应该有一个取消按钮返回表单,或者是一个允许提交表单的确定按钮

这是代码,我做了一些研究,它说使用一个循环,我对这个很陌生,所以我不知道该怎么做

<head>
<title>Exam entry</title>
<script language="javascript" type="text/javascript">

function validateForm() {
var result = true;
var msg="";
if (document.ExamEntry.name.value=="") {
msg+="You must enter your name \n";
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}

if(msg==""){
return result;
}
{
alert(msg)
return result;
}
}

</script>
</head>
<body>
<h1>Exam Entry Form</h1>
<form name="ExamEntry" method="post" action="success.html">
<table width="50%" border="0">
<tr>
<td id="name">Name</td>
<td><input type="text" name="name" /></td>
<tr>
<td id="subject">Subject</td>
<td><input type="text" name="subject" /></td>
</tr>
<tr>
<td id="examnumber">Examination Number</td>
<td><input type="text" name="examnumber" /></td>
</tr>
<tr>
<td><input type="radio" id="examtype" name="examtype" /> : GCSE<br />
<td><input type="radio" id="examtype" name="examtype"  /> : A2<br />
<td><input type="radio" id="examtype" name="examtype" /> : AS<br />
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();"    />  </td>
<td><input type="reset" name="Reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>

报考
函数validateForm(){
var结果=真;
var msg=“”;
if(document.ExamEntry.name.value==“”){
msg+=“您必须输入您的姓名\n”;
document.ExamEntry.name.focus();
document.getElementById('name').style.color=“红色”;
结果=假;
}
如果(msg==“”){
返回结果;
}
{
警报(msg)
返回结果;
}
}
报考表
名称
主题
考试号码
:GCSE
:A2
:AS

如下设置单选按钮的值:

<td><input type="radio" id="examtype" name="examtype" value="GCSE" /> : GCSE<br />
<td><input type="radio" id="examtype" name="examtype" value="A2" /> : A2<br />
<td><input type="radio" id="examtype" name="examtype" value="AS"/> : AS<br />
:GCSE
:A2
:AS
然后在函数中使用此脚本来验证窗体,或将其设置为函数,并从函数validateForm调用它:

var checked = null;
var inputs = document.getElementsByName('examtype');
for (var i = 0; i < inputs.length; i++) {
          if (inputs[i].checked) {
           checked = inputs[i];
           break;
   }
}
if(checked==null)
{
    alert('Please choose an option');
    return false;
}
else{
    return confirm('You have chosen '+checked.value+' is this correct?');
}
var checked=null;
var inputs=document.getElementsByName('examtype');
对于(变量i=0;i
如下设置单选按钮的值:

<td><input type="radio" id="examtype" name="examtype" value="GCSE" /> : GCSE<br />
<td><input type="radio" id="examtype" name="examtype" value="A2" /> : A2<br />
<td><input type="radio" id="examtype" name="examtype" value="AS"/> : AS<br />
:GCSE
:A2
:AS
然后在函数中使用此脚本来验证窗体,或将其设置为函数,并从函数validateForm调用它:

var checked = null;
var inputs = document.getElementsByName('examtype');
for (var i = 0; i < inputs.length; i++) {
          if (inputs[i].checked) {
           checked = inputs[i];
           break;
   }
}
if(checked==null)
{
    alert('Please choose an option');
    return false;
}
else{
    return confirm('You have chosen '+checked.value+' is this correct?');
}
var checked=null;
var inputs=document.getElementsByName('examtype');
对于(变量i=0;i
并添加到单选按钮(onclick=“return confirmation();”) 记住把这个放在你们所有人的单选按钮上 所以如果这不起作用

并添加到单选按钮(onclick=“return confirmation();”) 记住把这个放在你们所有人的单选按钮上
因此,如果这不起作用。

此代码运行良好或出现错误,请解释您面临的问题。此代码运行良好或出现错误,请解释您面临的问题。不,->-1,您的解决方案消耗宝贵的CPU周期。一旦你找到了检查过的收音机,你应该
for
循环中中断
。谢谢你提到@Kira,我已经更新了代码,但有一件事。。当您只有3-4个单选按钮时,这与CPU周期无关,而是与约定有关。感谢您更新您的答案。否,->-1,您的解决方案会消耗宝贵的CPU周期。一旦你找到了检查过的收音机,你应该
for
循环中中断
。谢谢你提到@Kira,我已经更新了代码,但有一件事。。当您只有3-4个单选按钮时,这与CPU周期无关,而是与约定有关。谢谢你更新你的答案。