HTMLFormElement
方法
requestSubmit()
requests that the form be submitted using a specific submit button.
htmlFormElement.requestSubmit(submitter);
submitter
可选
The submit button whose attributes describe the method by which the form is to be submitted. This may be either an
<input>
or
<button>
element whose
type
属性为
submit
.
If you omit the
submitter
parameter, the form element itself is used as the submitter.
None.
TypeError
submitter
is not a submit button.
NotFoundError
submitter
isn't a member of the form on which
requestSubmit()
was called. The submitter must be either a descendant of the form element or must have an
form
attribute referring to the form.
The obvious question is: Why does this method exist, when we've had the
submit()
method since the dawn of time?
The answer is simple.
submit()
submits the form, but that's all it does.
requestSubmit()
, on the other hand, acts as if a submit button were clicked. The form's content is validated, and the form is submitted only if validation succeeds. Once the form has been submitted, the
submit
event is sent back to the form object.
In the example below, the form is submitted by attempting to send the request using
requestSubmit()
if it's available. If a submit button with the ID
main-submit
is found, that's used to submit the form. Otherwise, the form is submitted with no
submitter
parameter, so it's submitted directly by the form itself.
If, on the other hand,
requestSubmit()
isn't available, this code falls back to calling the form's
submit()
方法。
let myForm = document.querySelector("form");
let submitButton = myForm.querySelector("#main-submit");
if (myForm.requestSubmit) {
if (submitButton) {
myForm.requestSubmit(submitButton);
} else {
myForm.requestSubmit();
}
} else {
myForm.submit();
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'requestSubmit()' 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 上的兼容性数据| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
requestSubmit()
|
Chrome 76 | Edge 79 | Firefox 75 | IE No | Opera 63 | Safari No | WebView Android 76 | Chrome Android 76 | Firefox Android No | Opera Android 54 | Safari iOS No | Samsung Internet Android 12.0 |
完整支持
不支持
HTMLFormElement
reportValidity()
requestSubmit()
reset()
submit()
BeforeUnloadEvent
DOMStringMap
ErrorEvent
GlobalEventHandlers
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers