Javascript 在提交事件中,什么都没有发生

Javascript 在提交事件中,什么都没有发生,javascript,css,xhtml,Javascript,Css,Xhtml,这是我的密码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script type = "text/

这是我的密码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <script type = "text/javascript">
          function validate(){
              var warnings = "some warnings";               
              document.getElementById("alert").innerHTML = warnings;
              document.getElementById("alert").styledisplay = "block";
              return true;
          }
        </script>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
      </head>
      <body>
        <div id = "alert"></div>
        <div id = wrapper>
          <h2>Enter Your Info</h2>
          <form name = "myForm" onSubmit = "javascript:validate()">
        <div>
          <label for = "username">User Name:</label>
          <input type = "text" name = "username" id = "username"/>
        </div>
        <div>
          <label for = "firstname">First Name:</label>
          <input type = "text" name = "firstname" id = "firstname"/>
        </div>
        <div>
          <label for = "lastname">Last Name:</label>
          <input type = "text" name = "lastname" id = "lastname"/>
        </div>
        <div>
          <label for = "password">Password:</label>
          <input type = "password" name = "password" id = "password"/>
         </div>
        <div>
          <input type = "submit" value = "submit" id = "submit"/>
        </div>
          </form>
        </div>
      </body>
    </html>
我遇到的问题是,当我点击提交按钮时,什么也没有发生。不知道为什么,我对java脚本和html还很陌生,但我读到的每一篇文章都说这应该有效。如果有任何帮助,我将不胜感激。

您总是返回true。因此,表格将提交。相反,请尝试以下方法:

<form name="myForm" onSubmit="return validate()">

只是补充一下。ifwarnings将根据您设置的警告初始状态使条件失败,或者在验证成功的情况下从validate函数中不设置任何内容返回空字符串或返回null、未定义等,或者只返回任何内容…

警报div当前的样式为display:none;尝试删除javascript:在验证之前
 function validate() {

     var warnings  = validate();

     document.getElementById("alert").innerHTML = warnings;
     document.getElementById("alert").styledisplay = "block";
     if(warnings) return false; //If any warning i don't want to submit.
     return true;
 }