zoukankan      html  css  js  c++  java
  • jQuery .submit()

    .submit()

    .submit( handler )Returns: jQuery

    Description: Bind an event handler to the "submit" JavaScript event, or trigger that event on an element.

    This method is a shortcut for .on( "submit", handler ) in the first variation, and .trigger( "submit" ) in the third.

    The submit event is sent to an element when the user is attempting to submit a form.

    It can only be attached to <form> elements. Forms can be submitted either by clicking an explicit <input type="submit">, <input type="image">, or <button type="submit">, or by pressing Enter when certain form elements have focus.

    Depending on the browser, the Enter key may only cause a form submission if the form has exactly one text field, or only when there is a submit button present. The interface should not rely on a particular behavior for this key unless the issue is forced by observing the keypress event for presses of the Enter key.

    For example, consider the HTML:

    <form id="target" action="destination.html">
      <input type="text" value="Hello there">
      <input type="submit" value="Go">
    </form>
    <div id="other">
      Trigger the handler
    </div>

    The event handler can be bound to the form:

    $( "#target" ).submit(function( event ) {
      alert( "Handler for .submit() called." );
      event.preventDefault();
    });

    Now when the form is submitted, the message is alerted. This happens prior to the actual submission, so we can cancel the submit action by calling .preventDefault() on the event object or by returning false from our handler. We can trigger the event manually when another element is clicked:

    $( "#other" ).click(function() {
      $( "#target" ).submit();
    });

    After this code executes, clicks on Trigger the handler will also display the message. In addition, the default submit action on the form will be fired, so the form will be submitted.

    The JavaScript submit event does not bubble in Internet Explorer.

    However, scripts that rely on event delegation with the submit event will work consistently across browsers as of jQuery 1.4, which has normalized the event's behavior. 

    Additional Notes:

    • As the .submit() method is just a shorthand for .on( "submit", handler ), detaching is possible using .off( "submit" ).
    • Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures. For a complete list of rules and to check your markup for these problems, see DOMLint.   
  • 相关阅读:
    实践是检验真理的唯一标准——《精益创业实战》让你的成功飞起来
    蛇年谈程序员发展之路
    蛇年,你使用Python语言编程了吗?
    由《30天自制操作系统》引发的漫画创作
    《七周七语言:理解多种编程范型》书评整理
    精益创业实战
    一本为程序员量身打造的面试逻辑题宝典——《程序员面试逻辑题解析》
    从1.5k到18k, 一个程序员的5年成长之路
    Comparator and Comparable及Collections and Collection的用法和区别
    线程同步
  • 原文地址:https://www.cnblogs.com/chucklu/p/11169449.html
Copyright © 2011-2022 走看看