submit event fires when a <form> is submitted.

冒泡 Yes (although specified as a simple event that doesn't bubble)
可取消 Yes
接口 SubmitEvent
事件处理程序特性 GlobalEventHandlers.onsubmit

注意, submit 事件激发在 <form> element itself, and not on any <button> or <input type="submit"> inside it. However, the SubmitEvent which is sent to indicate the form's submit action has been triggered includes a submitter property, which is the button that was invoked to trigger the submit request.

submit event fires when the user clicks a submit button ( <button> or <input type="submit"> ) or presses Enter while editing a field (e.g. <input type="text"> ) in a form. The event is not sent to the form when calling the form.submit() method directly.

注意: Trying to submit a form that does not pass validation triggers an 无效 event. In this case, the validation prevents form submission, and thus there is no submit 事件。

范例

此范例使用 EventTarget.addEventListener() to listen for form submit, and logs the current Event.timeStamp whenever that occurs, then prevents the default action of submitting the form.

HTML

<form id="form">
  <label>Test field: <input type="text"></label>
  <br><br>
  <button type="submit">Submit form</button>
</form>
<p id="log"></p>
					

JavaScript

function logSubmit(event) {
  log.textContent = `Form Submitted! Time stamp: ${event.timeStamp}`;
  event.preventDefault();
}
const form = document.getElementById('form');
const log = document.getElementById('log');
form.addEventListener('submit', logSubmit);
					

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'submit' in that specification.
工作草案 无变化
HTML 5.2
The definition of 'submit' in that specification.
推荐 无变化
HTML 5.1
The definition of 'submit' in that specification.
推荐 无变化
HTML5
The definition of 'submit' in that specification.
推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
submit event Chrome 1 Edge 12 Firefox 1 IE 9 Opera 8 Safari 3 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: