需要帮助检查单选按钮JavaScript的验证吗
我正在做一个GCSE计算课程任务,我试图在一组3个单选按钮上获得正确的验证。然而,单选按钮的代码(我从外部来源复制和粘贴,我现在不记得了)但是,该复制和粘贴的代码似乎覆盖了其他字段的验证(两个文本输入字段和一个数字输入字段)代码如下所示需要帮助检查单选按钮JavaScript的验证吗,javascript,html,Javascript,Html,我正在做一个GCSE计算课程任务,我试图在一组3个单选按钮上获得正确的验证。然而,单选按钮的代码(我从外部来源复制和粘贴,我现在不记得了)但是,该复制和粘贴的代码似乎覆盖了其他字段的验证(两个文本输入字段和一个数字输入字段)代码如下所示 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1 /DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1 /DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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 (document.ExamEntry.subject.value=="") {
msg+="You must enter the subject \n";
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}
if (document.ExamEntry.examnumber.value=="") {
msg+="You must enter the exam number \n";
document.ExamEntry.examnumber.focus();
document.getElementById('examnumber').style.color="red";
result = false;
}
if (document.ExamEntry.examnumber.value.length!=4) {
msg+="Your exam number must be exactly 4 digits \n";
document.ExamEntry.examnumber.focus();
document.getElementById('examnumber').style.color="red";
result = false;
}
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?');
}
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>
<tr>
<td id="subject">Subject</td>
<td><input type="text" name="subject" /></td>
</tr>
<tr>
<td id="examnumber">Exam Number</td>
<td><input type="number" name="examnumber" size="4" maxlength="4"></td>
</tr>
<tr>
<td><input type="radio" id="examtype" name="examtype" value="GCSE" /> : GCSE<br/>
<input type="radio" id="examtype" name="examtype" value="A2" /> : A2<br/>
<input type="radio" id="examtype" name="examtype" value="AS"/> : 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>
</body>
</html>
报考
您在HTML文档中仍然有一些错误:
- 重复的身份证
- 未关闭的
标签
- javascript中缺少“else”
- 重复的
标记
标记中的额外双引号
我修复了这些问题。我还将msg变量的警报移到选项检查上方,以便它们不会相互干扰。为了使第一个字段而不是最后一个字段聚焦,我添加了一个检查,以查看结果是否仍然为真:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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";
if(result) document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}
if (document.ExamEntry.subject.value=="") {
msg+="You must enter the subject \n";
if(result) document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}
if (document.ExamEntry.examnumber.value=="") {
msg+="You must enter the exam number \n";
if(result) document.ExamEntry.examnumber.focus();
document.getElementById('examnumber').style.color="red";
result = false;
}
if (document.ExamEntry.examnumber.value.length!=4) {
msg+="Your exam number must be exactly 4 digits \n";
if(result) document.ExamEntry.examnumber.focus();
document.getElementById('examnumber').style.color="red";
result = false;
}
if(msg != ""){
alert(msg);
return result;
}
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?');
}
}
</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>
<tr>
<td id="subject">Subject</td>
<td><input type="text" name="subject" /></td>
</tr>
<tr>
<td id="examnumber">Exam Number</td>
<td><input type="text" name="examnumber" size="4" maxlength="4"/></td>
</tr>
<tr>
<td><input type="radio" id="examtypeGCSE" name="examtype" value="GCSE" /> : GCSE<br/>
<input type="radio" id="examtypeA2" name="examtype" value="A2" /> : A2<br/>
<input type="radio" id="examtypeAS" name="examtype" value="AS"/> : 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>
</html>
报考
函数validateForm(){
var结果=真;
var msg=“”;
if(document.ExamEntry.name.value==“”){
msg+=“您必须输入您的姓名\n”;
if(result)document.ExamEntry.name.focus();
document.getElementById('name').style.color=“红色”;
结果=假;
}
if(document.ExamEntry.subject.value==“”){
msg+=“您必须输入主题\n”;
if(result)document.ExamEntry.subject.focus();
document.getElementById('subject').style.color=“red”;
结果=假;
}
if(document.ExamEntry.examnumber.value==“”){
msg+=“您必须输入考试编号\n”;
if(结果)document.ExamEntry.examnumber.focus();
document.getElementById('examnumber').style.color=“红色”;
结果=假;
}
if(document.ExamEntry.examnumber.value.length!=4){
msg+=“您的考试号码必须正好是4位数字\n”;
if(结果)document.ExamEntry.examnumber.focus();
document.getElementById('examnumber').style.color=“红色”;
结果=假;
}
如果(msg!=“”){
警报(msg);
返回结果;
}
检查的var=null;
var inputs=document.getElementsByName('examtype');
对于(变量i=0;i
:A2
:AS
ID属性对于文档是唯一的,您不能有3个ID为“examtype”的元素。此外,您正在通过ID引用元素:document.getElementById('examnumber')),但没有这样的元素,只有一个名为“examnumber”。您需要先修复这些错误…好的,更改了id,现在怎么办?仍然覆盖验证过程的其余部分