2012-08-13 49 views
0

我寫了一個JavaScript頁面用於驗證表單,然後重定向(form.action)到另一個頁面,但它只是重新加載頁面。這裏是我的代碼:Javascript驗證表單不重定向頁面

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<!-- InstanceBegin template="/Templates/nirsaplate_forms.dwt" codeOutsideHTMLIsLocked="false" --> 
<head> 
<!-- InstanceBeginEditable name="Title/Keywords" --> 
<title>TED CEU/PIC Tracking System</title> 
<meta name="keywords"> 
<script type="text/javascript"> 
var typesAndCovered={}; 
typesAndCovered['CEU']=['Philosphy & Theory','Programming','Management Techniques','Business Procedures','Facility Management, Planning & Design','Research & Evaluation','Legal Liability & Risk Management','Personal & Professional Qualities']; 
typesAndCovered['PIC']=['Leadership, governance and service','Education, training, scholarship and research']; 

function ChangeTypeList() 
{ 
var typeList=document.getElementById("Atype"); 
var coveredList=document.getElementById("typecovered"); 
var selType=typeList.options[typeList.selectedIndex].value; 
while (coveredList.options.length) 
    { 
    coveredList.remove(0); 
    } 
var types=typesAndCovered[selType]; 
if (types) 
    { 
    for (var i=0;i<types.length;i++) 
    { 
    var Atype=new Option(types[i],i); 
    coveredList.options.add(Atype); 
    } 
    } 
} 
</script> 
<script type="text/javascript"> 
<!-- 

function validateForm(form) { 

     if (form.Atype.value == "") { 
      alert("You must specify activity to track: CEU or PIC?"); 
      form.Atype.focus(); 
      return false; 
     } 
     if (form.DateEvent.value == "") { 
      alert("You must specify a date for this event."); 
      form.DateEvent.focus(); 
      return false; 
     } 
     if (form.NameEvent.value == "") { 
      alert("You must specify a name for this event."); 
      form.NameEvent.focus(); 
      return false; 
     } 
     if (form.NameSession.value == "") { 
      alert("You must specify a session name."); 
      form.NameSession.focus(); 
      return false; 
     } 
     if (form.UnitsEarned.value == "") { 
      alert("You must specify how many units you earned."); 
      form.UnitsEarned.focus(); 
      return false; 
     } 
     if (form.CEUPICType.value == "") { 
      alert("You must specify a CEU/PIC type."); 
      form.CEUPICType.focus(); 
      return false; 
     } 
     if (form.TEDEvent.value == "") { 
      alert("You must specify if this was a TED event or not."); 
      form.TEDEvent.focus(); 
      return false; 
     } 
     if (form.TypeCovered.value == "") { 
      alert("You must specify CEU Core Competency or PIC Category."); 
      form.TypeCovered.focus(); 
      return false; 
     } 

form.action = "display.asp"; 
form.submit(); 

    return true; 
} 

//--> 
</script> 
<!-- InstanceEndEditable --> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 
<body onload="ChangeTypeList();"> 
<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
    <tr align="center" valign="middle" bgcolor="#000033"> 
    <td>&nbsp;</td> 
    <td align="left" width="150"><a class="tedhome" href="http://www.ted.org">&nbsp;&nbsp;TED Home</a></td> 
    <td id="tedtop" width="450" height="20">&nbsp;</td> 
    <td width="150"></td> 
    <td>&nbsp;</td> 
    </tr> 
</table> 
<table width="750" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#ffffff"> 
    <tr valign="top" bgcolor="#003399" height="60"> 
    <td rowspan="2" colspan="2" height="80"><img src="images/ted_head_1.jpg" alt="TED" height="80" width="500" border="0"></td> 
    <td height="60"><img src="images/ted_head_2.jpg" alt="" height="60" width="250" border="0"></td> 
    </tr> 
    <tr valign="top" bgcolor="#003399" height="20"> 
    <td id="toplinks" align="right" valign="middle" height="20"><p></p></td> 
    </tr> 
    <tr bgcolor="#e3e3e3"> 
    <td colspan="3" align="center" height="20"><div id="tagline">&nbsp;</div></td> 
    </tr> 
<!-- Begin mainNav --> 
    <tr bgcolor="#003399"> 
    <td colspan="3" height="20">&nbsp;</td> 
    </tr> 
<!-- End mainNav --> 
</table> 
<table width="750" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#ffffff"> 
    <tr align="left" valign="top"> 
    <td> 
     <table width="750" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#ffffff"> 
     <tr valign="top"> 
      <td><!-- InstanceBeginEditable name="Content" --> 
      <div id="Content"><h1>TED CEU/PIC Tracking System</h1> 
      <p><font color="#cc0000">* Required field</font></p> 
      <form name="form" Method="post" onSubmit="return validateForm(form)"> 
      <table width="650" border="0" cellspacing="1" cellpadding="3" align="center" bgcolor="gray"> 
       <tr valign="top"> 
       <td colspan="2" bgcolor="#FFCC66"><h4>CEU/PIC Details:</h4></td> 
       </tr> 
       <tr valign="top"> 
       <td bgcolor="#e3e3e3"><p><strong><font color="#cc0000">*</font> Activity to Track:</strong></p></td> 
       <td bgcolor="#FFFFFF"><p><select name="Atype" id="Atype" onchange="ChangeTypeList();"> 
       <option value="">-- Activity Type --</option> 
       <option value="CEU">CEU</option> 
       <option value="PIC">PIC</option></select></p></td> 
       </tr> 
       <tr valign="top"> 
       <td bgcolor="#e3e3e3"><p><strong><font color="#cc0000">* </font>Date:</strong></p></td> 
       <td bgcolor="#FFFFFF"><input name="DateEvent" type="text" class="ctrl" id="DateEvent" size="10" maxlength="10"></td> 
       </tr> 
       <tr valign="top"> 
       <td bgcolor="#e3e3e3"><p><strong><font color="#cc0000">*</font> Name of Event:</strong></p></td> 
       <td bgcolor="#FFFFFF"><input name="NameEvent" type="text" id="NameEvent" size="50" maxlength="255"></td> 
       </tr> 
       <tr valign="top"> 
       <td bgcolor="#e3e3e3"><p><strong><font color="#cc0000">*</font> Name of Educational Session:</strong></p></td> 
       <td bgcolor="#FFFFFF"><input name="NameSession" type="text" id="NameSession" size="50" maxlength="255"></td> 
       </tr> 
       <tr valign="top"> 
       <td bgcolor="#e3e3e3"><p><strong><font color="#cc0000">*</font> CEUs/PICs Earned:</strong></p></td> 
       <td bgcolor="#FFFFFF"><input name="UnitsEarned" type="text" class="ctrl" id="UnitsEarned" size="50" maxlength="255"></td> 
       </tr> 
       <tr valign="top"> 
       <td bgcolor="#e3e3e3"><p><strong><font color="#cc0000">*</font> CEU/PIC Type:</strong></p></td> 
       <td bgcolor="#FFFFFF"><input name="CEUPICType" type="text" id="CEUPICType" size="25" maxlength="255"></td> 
       </tr> 
       <tr valign="top"> 
       <td bgcolor="#e3e3e3"><p><strong><font color="#cc0000">*</font> TED Event:</strong></p></td> 
       <td bgcolor="#FFFFFF"><p><select name="TEDEvent" id="TEDEvent"><option value="Yes" selected>Yes</option><option value="No">No</option></select></p></td> 
       </tr> 
       <tr valign="top"> 
       <td bgcolor="#e3e3e3"><p><strong><font color="#cc0000">*</font> CEU/PIC Core Competency/Category Covered:</strong></p></td> 
       <td bgcolor="#FFFFFF"><p><select id="typecovered"></select></p></td> 
       </tr> 
       <tr valign="top"> 
       <td colspan="2" align="center" bgcolor="#FFCC66"><p><br><b>Submit your CEU/PIC tracking.</b></p><p align="center">&nbsp;</p></td> 
       </tr> 
       <tr valign="top"> 
       <td colspan="2" align="center" bgcolor="#FFFFFF"><p><input type="submit" value="Submit Data" name="submit" ><input type="reset" name="reset" value="Reset"></p></td> 
       </tr> 
      </table> 
      </form> 
      </div> 
<!-- InstanceEndEditable --> 
      </td> 
     </tr> 
     </table> 
    </td> 
    </tr> 
</table> 
</body> 
<!-- InstanceEnd --> 
</html> 

任何幫助將是非常有益的。 謝謝。

回答

0

我的猜測是,你正在進入一個無限循環,而且它會導致「堆棧溢出」錯誤;)

嘗試for.action = ...

form.action = "display.asp"; 
// form.submit(); // this is what triggers validateForm again and again... 
0

您使用的形式後取出form.submit();中的功能validateForm(形式) TypeCovered,但實際ID爲typecovered(全部小寫),這將導致異常,使函數失敗

+0

這回答的問題。當改變到'正確的'區分大小寫時,一切都奏效了。 – technewbe 2012-08-13 19:22:53