2013-02-05 119 views
0

我無法弄清楚爲什麼我的函數無法在提交時被我的html表單調用。當我直接將javascript放在頭部時它工作正常,但是當它在它自己的.js文件中時它沒有。無法通過外部JS文件調用函數提交

我validate.js

function validateForm() 
{ 
if (document.forms[0].myName.value == "") 
    {alert("Name field cannot be empty."); 
     return false; 
    } // end if 
    if (document.forms[0].myEmail.value == "") 
    { 
    alert("Email field cannot be empty"); 
    return false; 
    } // end if 
    if (document.forms[0].myJob.value == "") 
    { 
     alert(" Job Description cannot be empty"); 
    return false; 
}//end if 
    if (document.forms[0].myPhone.value == "") 
    { 
     alert(" Phone Number cannot be empty"); 
     return false; 
    }//end if 
    alert("Name, email, job description and phone number are valid."); 
    document.forms[0].myName.value = document.forms[0].myName.value.toUpperCase(); 
    return true; 
} // end function validateForm 

而我的HTML

<title>Pasha the Painter Estimates</title> 
<link href="painter.css" rel="stylesheet" type="text/css" /> 
<link href="favicon.ico" rel="icon" type="images/x-icon" /> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script type=「text/javascript」 src=「validate.js」></script> 
</head> 
<body> 
<div id="container"> 
<h1 id="logo"><img src="painterlogo.gif" alt="Pasha the Painter" width="620" height="120" /></h1> 
<div id="leftcolumn"> 
    <ul> 
    <li><a href="index.html">Home</a></li> 
    <li><a href="services.html">Services</a></li> 
    <li><a href="testimonials.html">Testimonials</a></li> 
    <li><a href="estimates.html">Estimates</a></li> 
    </ul> 
</div> 
<div id="rightcolumn"> 
    <p>Request a Free Estimate.</p> 
    <form method="post" action="http://webdevfoundations.net/scripts/painter.asp" onsubmit="return validateForm();"> 
    <div class="myRow"> 
     <label class="labelCol" for="myName">Name: </label> 
     <input type="text" name="myName" id="myName" /> 
    </div> 
    <div class="myRow"> 
     <label class="labelCol" for="myEmail">E-mail: </label> 
     <input type="text" name="myEmail" id="myEmail" /> 
    </div> 
    <div> 
     <label class="labelCol" for="myJob">Description </label> 
     <textarea name="myJob" id="myJob" rows="2" cols="20"></textarea> 
    </div> 
    <div> 
     <label class="labelCol" for="myPhone">Phone Number: </label> 
     <input type="text" name="myPhone" id="myPhone" /> 
    <div class="mySubmit"> 
     <input type="submit" value="Free Estimates" /> 
    </div> 
    </form> 
    <div id="footer">Copyright &copy; 2011 Pasha the Painter<br /> 
    <a href="mailto:[email protected]">[email protected]</a> 
    </div> 
</div> 
</div> 
</body> 
</html> 

回答

1

你用錯了引號的跡象:

<script type=「text/javascript」 src=「validate.js」></script> 
      ^   ^ ^  ^

應該是:

<script type="text/javascript" src="validate.js"></script> 
      ^   ^ ^  ^ 
+0

所以我無法接受6分鐘的答案或直到15代表給予upvote,但你是對的。我一直在玩追趕,並卡住了,謝謝你的新鮮眼睛,我不知道我什麼時候會注意到。 再次感謝。 –

+0

@SpenserK,沒問題...很高興它是o.k.現在。並歡迎來到stackoverflow。 – gdoron

+0

感謝使用堆棧之前,當我陷入了Java和C +的困境,但這是第一次我提出了一個問題,如此快速的響應時間。 –