Submit () method and onsubmit event for form elements (GO)

Source: Internet
Author: User

    • 1. Elements of Name= "submit" appear in the form element
    • 2, Elemform.submit (); onsubmit events that do not touch the publication order
    • 3. Problems encountered when creating forms dynamically

The form element has a submit method and also has a onsubmit event handle to listen for form submissions. You can use the Elemform.submit () method to submit a form.

1. Elements of Name= "submit" appear in the form element

In this case elemform.submit () will not be able to submit the order because the form's original Submit method will be overwritten (Formelem.submit is a reference to that element).

2, Elemform.submit (); onsubmit events that do not touch the publication order

There is no reason why it is so stipulated in the standard.

Somewhat similar to this is the relationship between onfocus, onblur, and focus (), Blur (), which calls Elem.blur () or Elem.focus () to trigger onblur and onfocus events.

These provide a way for us to consider whether some of the UI components should trigger related events when they are called internally. For example, the dialog component, which has a OnOpen event, and the returned object also has an open () method, we must then consider whether the open () method needs to trigger the OnOpen () event.

3. Problems encountered when creating forms dynamically

One method that is used frequently is to invoke the form before it is submitted, depending on the return value of the Validate () function to determine whether the form needs to be committed.

<form onsubmit= "return validate ();" ></form>

However, if you want to dynamically add validation to a form, that is, the HTML code does not write onsubmit, and after the page is loaded with JavaScript to add a handler to the form, the problem comes. Let's say we've got the DOM node of the form, which is stored in the variable elemform, which is usually added handler:

var check = function () {    if (' OK ') {        return true;    } else {        return false;}    }; if (elemform.addeventlistener) {    Elemform.addeventlistener ("Submit", check, false);} else if ( elemform.attachevent) {    elemform.attachevent ("onsubmit", check);}

The problem arises: "return false;" in Firefox and Chrome. It is not possible to prevent forms from being submitted (in IE), which is why you should write "return check ()" In the onsubmit attribute, not just "check ()".

What is the reason? See ECMAScript Language Binding, which is explicitly written, "Object Eventlistener:this is an ECMAScript function reference. This method has no return value. The parameter is a event object ", meaning that the event listener has no return value. In another sense, AddEventListener can bind multiple listener functions for an element, the return value of an event listener function, and cannot be used as the return value of the entire event. You can use the following methods to resolve

function Check (EV) {    ev = EV | | window.event;//Event Object    if (ev.preventdefault) {//Standard browser        e.preventdefault ();    } else {//IE browser        window.event.returnValue = false;    }}

In fact, all of the fundamental because IE does not support DOM level 2.

Transferred from: http://www.cnblogs.com/rainman/archive/2011/09/05/2168162.html

Submit () method and onsubmit event for form elements (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.