GlobalEventHandlers.onformdata

翻译不完整。 请帮助我们翻译这篇文章!

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

语法

target.onformdata = functionRef;

Value

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

示例

// 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

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
onformdataChrome Full support 77Edge Full support 79Firefox Full support 72IE No support NoOpera Full support 64Safari No support NoWebView Android Full support 77Chrome Android Full support 77Firefox Android No support NoOpera Android Full support 55Safari iOS No support NoSamsung Internet Android No support No

Legend

Full support  
Full support
No support  
No support

另见