Home >Web Front-end >HTML Tutorial >html event attribute onsubmit triggered when submitting a form

html event attribute onsubmit triggered when submitting a form

黄舟
黄舟Original
2017-11-04 14:40:225974browse

Example

Execute a section of JavaScript when submitting the form:

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

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 ff9c23ada1bcecdd1a0fb5d5a0f18437.

Differences between HTML 4.01 and HTML5

None.

Syntax

<form onsubmit="script">

Attribute value

Value Description
script Script 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>

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>

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>

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!

Statement:
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