FormDataEvent

FormDataEvent interface represents a formdata event — such an event is fired on an HTMLFormElement object after the entry list representing the form's data is constructed. This happens when the form is submitted, but can also be triggered by the invocation of a FormData() 构造函数。

This allows a FormData object to be quickly obtained in response to a formdata event firing, rather than needing to put it together yourself when you wish to submit form data via a method like XMLHttpRequest (见 Using FormData objects ).

构造函数

FormDataEvent()
创建新的 FormDataEvent object instance.

特性

继承特性来自其父级接口 Event .

FormDataEvent.formData
Contains the FormData object representing the data contained in the form when the event was fired.

方法

Inherits methods from its parent interface, Event .

范例

// 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 (var value of data.values()) {
    console.log(value);
  }
  // submit the data via XHR
  var request = new XMLHttpRequest();
  request.open("POST", "/formHandler");
  request.send(data);
});
					

规范

规范 状态 Comment
HTML Living Standard
The definition of 'FormDataEvent' in that specification.
实时标准

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
FormDataEvent Chrome 77 Edge 79 Firefox 72 IE No Opera 64 Safari No WebView Android 77 Chrome Android 77 Firefox Android No Opera Android 55 Safari iOS No Samsung Internet Android 12.0
FormDataEvent() 构造函数 Chrome 77 Edge 79 Firefox 72 IE No Opera 64 Safari No WebView Android 77 Chrome Android 77 Firefox Android No Opera Android 55 Safari iOS No Samsung Internet Android 12.0
formData Chrome 77 Edge 79 Firefox 72 IE No Opera 64 Safari No WebView Android 77 Chrome Android 77 Firefox Android No Opera Android 55 Safari iOS No Samsung Internet Android 12.0

图例

完整支持
完整支持
不支持
不支持

另请参阅