html event attribute onsubmit triggered when submitting a form

黄舟
Release: 2017-11-04 14:40:22
Original
5907 people have browsed it

Example

Execute a section of JavaScript when submitting the form:

<form action="demo_form.asp" onsubmit="checkForm()">
Copy after login

Browser support

IE

Firefox

Chrome

Safari

Opera

All major browsers support onsubmit attribute.

Definition and Usage

The onsubmit attribute is triggered when the form is submitted.

onsubmit attribute is only used in

.

Differences between HTML 4.01 and HTML5

None.

Syntax

<form onsubmit="script">
Copy after login

Attribute value

ValueDescription
scriptScript to run when onsubmit occurs.

In daily development, it is often necessary to add verification before the form is submitted (verify whether the date format is correct, verify whether an input is empty...), the first thing that comes to mind is is the onsubmit event. When a submit type button is clicked, the first thing that is triggered is the onsubmit() event of the form. At this point, we can write our own verification. The code is as follows:

<form action="1.asp" method="post" name="form1"  onsubmit=“alert(‘执行了onsubmit事件’);return true;”>   
               <input type="submit" name="save" value="保存" />   
</form>
Copy after login

But if we directly call the submit() event of the form, onsubmit() will not be executed. The code is as follows:

<form action="1.asp" method="post" name="form1"  onsubmit=“alert(‘执行了onsubmit事件’);return true;”>   ”>   
               <input type="button" name="save" value="保存" onclick=”this.form.submit();” />   
</form>
Copy after login

If we want to call The onsubmit event of the form can only be called. Only in this way can this event be executed:

<form action="1.asp" method="post" name="form1"  onsubmit=“alert(‘执行了onsubmit事件’);return true;”>   ”>   
               <input type="button" name="save" value="保存" onclick=”this.form.onsubmit();” />   
</form>
Copy after login

The above is the detailed content of html event attribute onsubmit triggered when submitting a form. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template