0

送信時に自分の関数が自分の 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”&gt;</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>
4

1 に答える 1

1

間違った引用符を使用しています:

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

次のようにする必要があります。

<script type="text/javascript" src="validate.js"></script>
             ^               ^     ^           ^    
于 2013-02-05T09:51:41.780 に答える