blob: 1909ba0ef90321a4e17ef8fe23467b048d3fd180 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
---
title: GlobalEventHandlers.onsubmit
slug: Web/API/GlobalEventHandlers/onsubmit
tags:
- API
- DOM
- Event
- Event Handler
- HTML
- 事件
- 事件接收器
- 属性
translation_of: Web/API/GlobalEventHandlers/onsubmit
---
<div>{{ ApiRef("HTML DOM") }}</div>
<p>{{domxref("GlobalEventHandlers")}} 的 <code><strong>onsubmit</strong></code> 属性是一个处理 {{event("submit")}} 的 {{domxref("EventHandler")}}。</p>
<p><code>submit</code> 事件会在用户点击提交按钮(<code><input type="submit"/></code> 元素)提交表单时触发。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox"><em>target</em>.onsubmit = <em>functionRef</em>;</pre>
<h3 id="参数">参数</h3>
<p><code>functionRef</code> 是一个函数名或 <a href="/xh-CN/docs/Web/JavaScript/Reference/Operators/function">函数表达式</a>。The function receives a {{domxref("FocusEvent")}} object as its sole argument.</p>
<h2 id="例子">例子</h2>
<p>This example demonstrates {{domxref("GlobalEventHandlers.oninvalid", "oninvalid")}} and <code>onsubmit</code> event handlers on a form.</p>
<h3 id="HTML">HTML</h3>
<pre class="brush: html"><form id="form">
<p id="error" hidden>Please fill out all fields.</p>
<label for="city">City</label>
<input type="text" id="city" required>
<button type="submit">Submit</button>
</form>
<p id="thanks" hidden>Your data has been received. Thanks!</p></pre>
<h3 id="JavaScript">JavaScript</h3>
<pre class="brush: js">const form = document.getElementById('form');
const error = document.getElementById('error');
const city = document.getElementById('city');
const thanks = document.getElementById('thanks');
city.oninvalid = invalid;
form.onsubmit = submit;
function invalid(event) {
error.removeAttribute('hidden');
}
function submit(event) {
form.setAttribute('hidden', '');
thanks.removeAttribute('hidden');
// For this example, don't actually submit the form
event.preventDefault();
}</pre>
<h3 id="Result">Result</h3>
<p>{{EmbedLiveSample("Example")}}</p>
<h2 id="规范">规范</h2>
<table class="spectable standard-table">
<tbody>
<tr>
<th scope="col">规范</th>
<th scope="col">状态</th>
<th scope="col">备注</th>
</tr>
<tr>
<td>{{SpecName('HTML WHATWG','webappapis.html#handler-onsubmit','onsubmit')}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<p>{{Compat("api.GlobalEventHandlers.onsubmit")}}</p>
</div>
<h2 id="参见">参见</h2>
<ul>
<li>{{event("submit")}} 事件</li>
</ul>
|