无法从外部JS文件调用函数来提交onsubmit

时间:2013-02-05 09:49:42

标签: javascript html

我无法弄清楚为什么我的函数无法在提交时被我的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 &copy; 2011 Pasha the Painter<br />
  <a href="mailto:yourfirstname@yourlastname.com">yourfirstname@yourlastname.com</a>
  </div>
</div>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您使用的是错误的引号:

<script type=“text/javascript” src=“validate.js”></script>
             ^               ^     ^           ^

应该是:

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