我无法弄清楚为什么我的函数在提交时无法被我的 html 表单调用。当我将 javascript 直接放在头部时,它可以正常工作,但是当它位于自己的 .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 © 2011 Pasha the Painter<br />
<a href="mailto:yourfirstname@yourlastname.com">yourfirstname@yourlastname.com</a>
</div>
</div>
</div>
</body>
</html>