JavaScript DHTML/jQuery/Form Submit

Материал из Web эксперт
Перейти к: навигация, поиск

Cancel submit event by returning false

   

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
        
            $("form").submit(function() {
                alert("submit");
                  return false;
            });

        });
    </script>
  </head>
  <body>
    <body>
    <form action="javascript:alert("success!");">
        <div>
          <input type="text" />
          <input type="submit" />
        </div>
     </form>
     <div></div>

    </body>
</html>



Submit event

    
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
        
            $("form").submit(function() {
                alert("submit");
                  return false;
            });

        });
    </script>
  </head>
  <body>
    <body>
    <form action="javascript:alert("success!");">
        <div>
          <input type="text" />
          <input type="submit" />
        </div>
     </form>
     <div></div>

    </body>
</html>



To trigger the submit event on the first form on the page

 
$("form:first").submit();



Turn off submit action

    
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("form").submit(function () { return false; }); 
        
        
    });
    </script>
  </head>
  <body>
     <form>
      <input type="text" />
    </form>
  </body>
</html>



Validate form and output message

 
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
        
            $("form").submit(function() {
              if ($("input:first").val() == "asdf") {
                $("span").text("Validated...").show();
                return true;
              }
              $("span").text("Not valid!").show().fadeOut(1000);
              return false;
            });
        });
    </script>
  </head>
  <body>
    <body>
          <form action="javascript:alert("success!");">
              <input type="text" />
              <input type="submit" />
          </form>
          <span></span>
    </body>
</html>