Common attribute Memo in JS

Source: Internet
Author: User
Tags event listener

.

OnSubmit is generally used for validation, and is used to control the form submission.

Before using data submission basically use AJAX to do data submission, in the data validation, only through the data validation, will be post/get data to the corresponding API file, but this involves the image upload, can only use form form submission, but this front desk JS judge when there is a problem. The form form is automatically submitted after JS verification.


How do I prevent forms from being automatically submitted?

<form name= "Form1" onsubmit= "return Check ()" method= "POST" action= "search_result.php" >

I'm going to say onsubmit=. "Return check ()" What is this for? First, there is a onsubmit attribute in the form tag, and he has two values, respectively, true and false, if true, This page jumps to the search_result.php page of the Action property, otherwise it does not jump. So return a value of TRUE or false with return.

That is, the value of onsubmit can determine whether the Action property is triggered.

Form form another event-onclick

<form name= "Form1" onclick= "return check ();" method= "Post" action= "search_result.php" >

JS Method:

function Check () {

Judge..

return false:

}

The difference between onclick and onsubmit:


OnSubmit is used on a form (and can only be a form), and is triggered before the form is submitted.
The OnClick is used on a control such as a button to trigger a click event.

When used as data validation, you can choose to validate in the onclick on the Submit button, which can be verified in onsubmit. But from the event firing sequence, the onclick is earlier. The order is:

User Click button, onclick, if the onclick returns a valid or unhandled onclick then submits the form, onsubmit, if onsubmit is not processed or returns True, commits, or cancels the commit.

A return of false in onsubmit causes the form to be submitted, and false in the onclick will cause the Click to be judged invalid and will not cause the form to be submitted.


Why use the return function?

Form 1:<form name= "Form1" onsubmit= "return check ();" method= "Post" action= "search_result.php" >

Form 2:<form name= "Form2" onsubmit= "Check ()" method= "POST" action= "search_result.php" >

JS Method:

function Check () {

Judge..

return false:

}

———————————————————————-

The check method of the above two forms returns false, so that the form can block the automatic submission of the form form action?

In fact: Only form 1 can be in IE and Firefox, do the form of the form itself submitted

Form 2 can prevent the form form from being automatically submitted under IE, but in Firefox it is not possible to prevent automatic submission from the form.

Reason: See ECMAScript Language Binding, which explicitly writes that the event listener has no return value. In fact, all the fundamental because IE does not support DOM level 2, IE and Firefox incompatible, resulting in check cannot prevent form submission.

We can understand this: the return value of check is False, then return check () ==return false is to block the default event (that is, the onsubmit event). And if just check () =false that is, and this false is only in check inside interrupt to the following JS execution, rather than the default event of this form block.


The function of return

In most cases, false is returned for the event handler to prevent the default event behavior

Return True is the equivalent of the execution character

--------------------------------------------------Other Events------------------------------------------------

Window event (Windows events)

Valid only in the body and frameset elements.

Properties value Description
OnLoad Script Execute script when document is loaded
OnUnload Script Execute script when document is removed
Form elements event (form element events)

Valid only in form elements.

Properties value Description
OnChange Script Execute script when element changes
OnSubmit Script Execute script when form is submitted
OnReset Script Execute script when form is reset
Onselect Script Execute script when element is selected
Onblur Script Execute script when element loses focus
onfocus Script Execute script when element gets focus
Images event (Image events)

This property can be used with the IMG element:

Properties value Description
Onabort Script Execute script when image load is interrupted
Keyboard event (Keyboard events)

Not valid in the following elements: base, BDO, BR, Frame, frameset, head, HTML, iframe, Meta, param, script, style, and title element.

Properties value Description
OnKeyDown Script Execute script when keyboard is pressed
onkeypress Script Execute script when keyboard is pressed and released
OnKeyUp Script Execute script when keyboard is released
Mouse events (Mouse events)

Not valid in the following elements: base, BDO, BR, Frame, frameset, head, HTML, iframe, Meta, param, script, style, and title element.

Properties value description
oncli CK Script Execute script when mouse is clicked
ondblclick script execute when Mouse is double-clicked Script
onmousedown script execute script when mouse button is pressed
onmous emove Script execute script When mouse pointer is moved
onmouseout script when mouse pointer Execute script when moving out of an element
onmouseover script execute script when the mouse pointer hovers over an element
onmouseup Script Execute script when mouse button is released

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.