HTMLFormElement.requestSubmit() - Web APIs 编辑

The HTMLFormElement method requestSubmit() requests that the form be submitted using a specific submit button.

Syntax

htmlFormElement.requestSubmit(submitter);

Parameters

submitter Optional

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 attribute is submit.

If you omit the submitter parameter, the form element itself is used as the submitter.

Return value

None.

Exceptions

TypeError
The specified submitter is not a submit button.
NotFoundError
The specified 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.

Usage notes

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.

Examples

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() method.

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();
}

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'requestSubmit()' in that specification.
Living Standard

Browser compatibility

BCD tables only load in the browser

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:116 次

字数:4563

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文