SubmitEvent.submitter - Web APIs 编辑

The read-only submitter property found on the SubmitEvent interface specifies the submit button or other element that was invoked to cause the form to be submitted.

Syntax

let submitter = submitEvent.submitter;

Value

An element, indicating the element that sent the submit event to the form. While this is often an <input> element whose type or a <button> whose type is submit, it could be some other element which has initiated a submission process.

If the submission was not triggered by a button of some kind, the value of submitter is null.

Examples

In this example, a shopping cart may have an assortment of different submit buttons depending on factors such as the user's settings, the shop's settings, and any minimum or maximum shopping card totals established by the payment processors. Each of the submit elements' id is used to identify which payment processor the button corresponds to.

let form = document.querySelector("form");
form.addEventListener("submit", (event) => {
  let submitter = event.submitter;
  let handler = submitter.id;

  if (handler) {
    processOrder(form, handler);
  } else {
    showAlertMessage("An unknown or unaccepted payment type was selected. Please try again.", "OK");
  }
});

The handler ID is obtained by using the submit event's submitter property to get the submit button, from which we then get the ID. With that in hand, we can call a processOrder() function to handle the order, passing along the form and the handler ID.

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'SubmitEvent.submitter' in that specification.
Living Standard

Browser compatibility

BCD tables only load in the browser

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

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

发布评论

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

词条统计

浏览:62 次

字数:3647

最后编辑:7 年前

编辑次数:0 次

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