aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/globaleventhandlers/onsubmit
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/globaleventhandlers/onsubmit
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/globaleventhandlers/onsubmit')
-rw-r--r--files/zh-cn/web/api/globaleventhandlers/onsubmit/index.html101
1 files changed, 101 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/globaleventhandlers/onsubmit/index.html b/files/zh-cn/web/api/globaleventhandlers/onsubmit/index.html
new file mode 100644
index 0000000000..1909ba0ef9
--- /dev/null
+++ b/files/zh-cn/web/api/globaleventhandlers/onsubmit/index.html
@@ -0,0 +1,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>&lt;input type="submit"/&gt;</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">&lt;form id="form"&gt;
+ &lt;p id="error" hidden&gt;Please fill out all fields.&lt;/p&gt;
+
+ &lt;label for="city"&gt;City&lt;/label&gt;
+ &lt;input type="text" id="city" required&gt;
+
+ &lt;button type="submit"&gt;Submit&lt;/button&gt;
+&lt;/form&gt;
+&lt;p id="thanks" hidden&gt;Your data has been received. Thanks!&lt;/p&gt;</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>