簡體   English   中英

HTML表單-通過onsubmit事件處理程序調用JavaScript

[英]HTML Forms - Calling JavaScript via the onsubmit event handler

我正在創建一個簡單的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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM