2014-05-01 27 views
0

我正在处理一个HTML文档,它是考试提交表单。我完成了几乎所有的设置任务,但是我在验证单选按钮时遇到了问题。我已经搜索了stackoverflow然而,答案不符合问题的标准。我完成的代码如下所示。单选按钮验证(警告框)确认和拒绝

我有这个任务的加粗部分麻烦:

添加一组单选按钮的窗体接受输入,如GCSE的水平,或A2。 编写一个函数,在警告框中显示用户输入的级别,以便可以确认或拒绝级别。

下面是工作的代码,不包括验证单选按钮。 Seperatley,我的代码在执行到函数validateForm()时不起作用。

<html> 
<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 (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.examno.value == "") { 
msg += "You must enter your Examination Number \n"; 
document.ExamEntry.examno.focus(); 
document.getElementById('examinationno').style.color = "red"; 
result = false; 
} 
if (document.ExamEntry.examno.value.length!== 4) { 
      msg+="Your Examination Number should be 4 digits.\n"; 
      document.ExamEntry.examno.focus(); 
      document.getElementById('examinationno').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></tr> 
     <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="examinationno">Examination Number</td> 
      <td> 
       <input type="text" name="examno" /> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <input type="radio" name="examtype" value="GCSE" />GCSE</td> 
     </tr> 
     <tr> 
      <td> 
       <input type="radio" name="examtype" value="AS" />AS</td> 
     </tr> 
     <tr> 
      <td> 
       <input type="radio" name="examtype" value="A2" />A2</td> 
     </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> 

基于当前的代码,为什么不将以下时加入到功能?:

if(document.getElementById('GCSE').checked) 
{examtype = "GCSE"; 
} 
if(document.getElementById('AS').checked) 
{examtype = "AS"; 
} 
if(document.getElementById('A2').checked) 
{examtype = "A2"; 
} 

if(confirm("You have selected"+examtype+.Continue?")){ 

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

alert(msg) 
return result; 
} 
} 

else{ 
alert("Action cancelled"); 
return false; 
} 
+0

这是一个家庭作业问题另外,由SO强调的确认信息有错误 – teynon

+0

这不是一个家庭作业问题,这是一个研究任务,在这个任务中,互联网的使用是必不可少的。什么是确认信息的错误?我也不是熟悉'SO'?感谢您的回复 – kylerp

+0

我不确定在JavaScript中哪里获得了一个“需要使用互联网的研究任务”,但是这个任务看起来很像是一个家庭作业问题,因此我看到你是一个新用户,所以我没有任何表明这是或不是一个家庭作业问题。所以,我只是指出错误在哪里和h你可以弄明白。看看'if(confirm ...){'并观看文本的颜色。然后看看引号。 – teynon

回答

0
// Retrieving the checked radio button value 
function getRadioValue (frmName, rbGroupName) 
{ 
    var radios = document[frmName].elements[rbGroupName]; 
    for (var i=0; i <radios.length; i++) 
    { 
     if (radios[i].checked) 
     { 
      return radios[i].value;  
     } 
    } 

    return false; 
} 

调用getRadioValue()内部validateForm()验证

// examtype gets false or GCSE or AS or A2 
var examtype = getRadioValue ("ExamEntry", "examtype"); 
+1

考虑到调用者已经知道表单和单选按钮组名,可能更容易传递集合:'getRadioValue(radioButtonCollection)'。 :-) – RobG

+0

谢谢你,在添加到当前函数validateForm()时,似乎有些冲突。只需调用var examtype = getRadioValue(“ExamEntry”,“examtype”);在函数validateForm()中导致验证通过成功页面。 – kylerp

0

“C ontinue“是一个关键字。所以,你必须把它包在单引号(“)的if语句,我做了一些改变我无法得到的结构。我希望这是你在找什么..

if(confirm("You have selected"+examtype+". Continue?")){ 

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

    else{ 
    alert("Action cancelled"); 
    return false; 
    } 
}