FormDataEvent: formData 属性

FormDataEvent 接口的 formData 只读属性包含一个 FormData 对象,该对象表示事件触发时表单中包含的数据。

一个 FormData 对象。

示例

js
// grab reference to form

const formElem = document.querySelector("form");

// submit handler

formElem.addEventListener("submit", (e) => {
  // on form submission, prevent default
  e.preventDefault();

  // construct a FormData object, which fires the formdata event
  new FormData(formElem);
});

// formdata handler to retrieve data

formElem.addEventListener("formdata", (e) => {
  console.log("formdata fired");

  // Get the form data from the event object
  let data = e.formData;
  for (const value of data.values()) {
    console.log(value);
  }

  // submit the data via XHR
  const request = new XMLHttpRequest();
  request.open("POST", "/formHandler");
  request.send(data);
});

规范

规范
HTML 标准
# the-formdataevent-interface:dom-formdataevent-formdata-2

浏览器兼容性

BCD 表格仅在启用 JavaScript 的浏览器中加载。

另请参见