GlobalEventHandlers.onformdata

GlobalEventHandlers 混入对象(mixin)的属性onformdata 是用于处理 formdata 事件的, 它在整个列表展示所构建的表单数据之后被触发。触发会发生在表单发送时,但也可能由对某个FormData() 结构体的调用所触发。onformdataHTMLFormElement上有效。

语法

target.onformdata = functionRef;

Value

functionRef 是一个函数名或者称为 function expression. 此函数接受一个FormDataEvent (en-US)对象作为其唯一表达式参数。

示例

// 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.onformdata = (e) => {
  console.log('formdata fired');

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

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

规范

规范 状态 备注
HTML Living Standard
onformdata
Living Standard

浏览器兼容性

BCD tables only load in the browser

另见