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.
<form id="form"> <label>Test field: <input type="text"></label> <br><br> <button type="submit">Submit form</button> </form> <p id="log"></p>
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);
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持