2011-05-23 232 views
0

我的JavaScript正在工作,現在不是。JavaScript突然停止工作

它只是爲了驗證表單

我開始運用我的模板上的應用程序,它似乎在某些時候它只是停止......有什麼可以做的任何想法?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> 
<html> 
<head> 
    <title>Create Profile</title> 

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
    <link rel="stylesheet" type="text/css"href="style.css"> 

    <script type="text/javascript"> 
     function validateForm() 
     { 
      var firstName=document.forms["register"]["firstName"].value; 
      var lastName=document.forms["register"]["lastName"].value; 
      var UserName=document.forms["register"]["UserName"].value; 
      var Password=document.forms["register"]["Password"].value; 
      var description=document.forms["register"]["description"].value; 
      var DOB=document.forms["register"]["DOB"].value; 
      var likes=document.forms.register.elements['likes[]']; 
      var image=document.forms["register"]["image"].value; 

      var likesCheck = 0; 
      for (var i = 0; i < likes.length; i++) 
      { 
       if (likes[i].checked) 
       { 
        likesCheck++; 
       } 

      } 
      if (likesCheck == 0) 
      { 
       alert("likes must be selected"); 
       return false; 
      } 

      if (firstName==null || firstName=="") 
      { 
       alert("First Name must be valid."); 
       return false; 
      }  
      else if (lastName==null || lastName=="") 
      { 
       alert("Last Name must be valid."); 
       return false; 
      } 
      else if (UserName==null || UserName=="") 
      { 
       alert("UserName must be valid."); 
       return false; 
      }   
      else if (DOB==null || DOB=="") 
      { 
       alert("DOB must be valid."); 
       return false; 
      } 
      else if (Password==null || Password=="") 
      { 
       alert("Password must be valid."); 
       return false; 
      } 
      else if (image==null || image=="") 
      { 
       alert("image must be valid."); 
       return false; 
      } 
      else if (description==null || description=="") 
      { 
       alert("description must be valid."); 
       return false; 
      } 

     } 

</script> 
</head> 

<body> 
<div id="container"> 
<div id="header"> 
    <div class="title"><a href="index.php">The Griffith Dating Connection</a></div> 
    <div class="subtitle">find your &quot;study buddy&quot;</div> 
    <div class="intro-part1">Where Griffith singles meet.</div> 
    <div class="intro-part2"></div></div> 
<div id="nav"> 

<div id="nav-in-top"> 

</div> 

<div id="nav-in-middle"> 
<p class="title2">Navigation</p><br> 

<span> 
<a class="menu" href="index.php">Home</a> 
<a class="menu" href="allusers.php">View All Users</a> 
<a class="menu" href="doc.html">Documentation</a> 
</span> 
<p>&nbsp;</p> 

</div> 
<div id="nav-in-bottom"></div> 
</div> 
<div id="content"> 
<p class="title2"><b>Dating</b></p> 
<br> 

</div> 
<div id="box"> 


<form name="register" method="post" action="profiles.php" onSubmit="return validateForm()" enctype="multipart/form-data" class="classform"> 
     <table style="margin: 0 auto;" class="bordered"> 

     <tr> 
      <td class="col1"> First Name </td> 
      <td class="col2"> <input type="text" name="firstName" size=30> </td> 
     </tr> 
     <tr> 
      <td class="col1"> Last Name </td> 
      <td class="col2"> <input type="text" name="lastName" size=30> </td> 
     </tr> 
     <tr> 
      <td class="col1"> Username </td> 
      <td class="col2"> <input type="text" name="UserName" size=30> </td> 
     </tr> 
     <tr> 
      <td class="col1">Password </td> 
      <td class="col2"> <input type="password" name="Password" size=30> </td> 
     </tr> 
     <tr> 
      <td class="col1"> Gender </td> 
      <td class="col2"> 
      <input type="radio" name="gender" value="Male" checked>Male<br> 
      <input type="radio" name="gender" value="Female">Female<br> 
     </tr> 
     <tr> 
       <td class="col1"> Date of Birth </td> 
       <td class="col2"> <input name="DOB" type="text" value="YYYY/MM/DD" size=30> </td> 

     </tr> 
     <tr> 
      <td class="col1"> Email </td> 
      <td class="col2"> <input type="text" name="email" size=30> </td> 
     </tr> 
     <tr> 
      <td class="col1"> Description </td> 
      <td class="col2"> <textarea name="description" cols="60" rows="10"></textarea> </td> 
     </tr> 
     <tr> 
      <td class="col1"> Likes </td> 
      <td class="col2"> 
           <input type="checkbox" name="likes[]" value="1">Swimming 
           <input type="checkbox" name="likes[]" value="2">Dancing 
           <input type="checkbox" name="likes[]" value="3">walking 
           <input type="checkbox" name="likes[]" value="4">making pizza 
           <input type="checkbox" name="likes[]" value="5">wrestling 
           <input type="checkbox" name="likes[]" value="6">ddddd 
           <input type="checkbox" name="likes[]" value="7">ddddd 
           <input type="checkbox" name="likes[]" value="8">ddddd 
           <input type="checkbox" name="likes[]" value="9">ddddd 
           <input type="checkbox" name="likes[]" value="10">sss 
           <input type="checkbox" name="likes[]" value="11">system 
           <input type="checkbox" name="likes[]" value="12">sysss 
           <input type="checkbox" name="likes[]" value="13">newer 
           <input type="checkbox" name="likes[]" value="14">newer 
          </td> 
     </tr> 
     <tr> 
      <td class="col1"> Create a new like</td> 
      <td class="col2"> 
       <input type="text" name="newlike" size=30><br> 
      </td> 

     </tr> 
     <tr> 
      <td class="col1">Profile Picture</td> 
      <td class="col2"> 
       <input type="file" name="file" id="file"> 
      </td> 
     </tr> 

     <tr> 
      <td colspan=2 style="text-align: center"> 
       <input type="submit" name="submit" value="Submit"> 
       <input type="reset" name="reset" value="Reset"> 
      </td> 

     </tr> 
     </table> 
     </form> 
</div> 

<div id="footer"> Josip Zirdum Griffith Student Number &quot;s2794971&quot; </div> 
</div> 
</body> 
</html> 

我收到此錯誤:

我有固定它。問題是我要求「圖像」,但我命名錶單元素「文件」。忘記我最近做了這個。對不起,浪費你的時間。但感謝的調試技巧:)

+4

有了這樣的問題,最好的地方開始是「發生了什麼變化? – 2011-05-23 14:28:35

+0

非常多添加模板。對我來說它似乎onSubmit不再在窗體函數中工作...不知道這意味着什麼。 – 2011-05-23 14:29:35

+0

這可能與添加觸發標準模式的Doctype有關,這意味着您所做的錯誤未被通過......但通過整個腳本進行跟蹤將是一項乏味的工作。你在JS調試器中得到什麼錯誤信息? – Quentin 2011-05-23 14:29:56

回答

6

你的JS正在尋找Description但你的表單控件被命名爲description

(可能有其他錯誤,但是這是目前導致JS把錯誤出一個,而不是返回false)

+0

我已經完成了它現在仍然無法使用。 – 2011-05-23 14:36:37

+4

正如我所說,可能還有其他錯誤。下一個是你正在尋找一個名爲image的表單控件,並且沒有一個。認真**看一下JS調試器的輸出**。 – Quentin 2011-05-23 14:39:50

+0

我修復了它。問題是我要求「圖像」,但我命名錶單元素「文件」。忘記我最近做了這個。對不起,浪費你的時間。但是,感謝您的調試技巧:) – 2011-05-23 14:48:12

2

我給你一個建議,

您的JavaScript看起來不錯,但如果你不是,Firefox擴展名爲Firebug的,你應該使用,打開它爲您的網頁,它會如果您有任何問題,可向您展示錯誤,並將幫助您進行網絡開發。

你可以得到它http://getfirebug.com/

,並添加回真實的;在你的方法結束

+1

更多的評論比答案,但聲音的建議。 – Orbling 2011-05-23 14:32:58

+0

+1。實際上,這是兩點建議...... ;-) – Spudley 2011-05-23 14:33:41

+2

OP爲什麼要添加'返回true;'?這沒有必要。此外,這應該是一個評論,而不是一個答案。 – 2011-05-23 14:33:52

4
Error: document.forms.register.Description is undefined 

您在這裏稱之爲 - 資本d:

var Description=document.forms["register"]["Description"].value; 

將其更改爲

var Description=document.forms["register"]["description"].value; 
+0

我已經做到了這一點,但它仍然無法正常工作。 – 2011-05-23 14:36:00

+0

錯誤:[異常...]'當調用方法時,JavaScript組件沒有名爲「getInterface」的方法:[nsIInterfaceRequestor :: getInterface]「nsresult:」0x80570030(NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED)「位置:」「data:no ] – 2011-05-23 14:38:05