HTML Forms - 通过 onsubmit 事件处理程序调用 JavaScript

HTML Forms - Calling JavaScript via the onsubmit event handler

本文关键字:程序 调用 JavaScript 事件处理 onsubmit Forms 通过 HTML      更新时间:2023-09-26

我正在创建一个简单的HTML表单,并通过onsubmit事件处理程序调用JavaScript文件。奇怪的是,每当我单击提交按钮时,我的JS文件都不会触发。帮助?

**更新的代码

这是我对精简HTML文件的内容:

<html>
<form name="form01" id="form01" action="http://itins3.madisoncollege.edu/echo.php"
      method="post" onsubmit="return checkAllTextBoxes();">
      <label for="actualFirstName" class="setWidth">First Name:</label>
      <input type="text" name="actualFirstName" id="actualFirstName" />
      <input type="submit" value="Send Form" />
</form>
</html>
<script src="/javaScriptFiles/newArtist.js" type="text/javascript"></script>

这是我的JS文件:

function checkAllTextBoxes()
{
     if (document.form01.actualFirstName.value.length < 2)
     {
          alert("First name is too short- must be at least two characters or more.");
          return false;
     }
     return true;
}

我一直在试图找出出了什么问题,但似乎在我的代码中找不到错误。尝试了JSHint,Firebug(FireFox),甚至HTML在线验证器,没有出现错误。另一双编码眼睛将是一个很大的帮助。谢谢。

<form name="form01" id="form01" method="post">
    <label for="actualFirstName" class="setWidth">First Name:</label>
    <input type="text" name="actualFirstName" id="actualFirstName" />
    <input type="submit" value="Send Form" onClick="return checkAllTextBoxes();" />
</form>
<Script>
function checkAllTextBoxes()
{
     if (document.form01.actualFirstName.value.length < 2)
     {
          alert("First name is too short- must be at least two characters or more.");
          return false;
     }
     else{
         document.form01.action = "http://itins3.madisoncollege.edu/echo.php";
         document.form01.submit;
         return true;
}
</script>

你必须将你的js文件链接到你的html。在 html 页面底部的结束标记后添加链接

这是我尝试过的,它有效..

function checkAllTextBoxes()
{
     if (document.form01.actualFirstName.value.length < 2)
     {
          alert("First name is too short- must be at least two characters or more.");
          return false;
     }
     return true;
}
<html>
<form name="form01" id="form01" action="http://itins3.madisoncollege.edu/echo.php"
      method="post" onsubmit="return checkAllTextBoxes();">
      <label for="actualFirstName" class="setWidth">First Name:</label>
      <input type="text" name="actualFirstName" id="actualFirstName" />
      <input type="submit" value="Send Form" />
</form>
</html>
<script src="myscripts.js"></script>