FormDataEvent: formData-Eigenschaft

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

We’d love to hear your thoughts on the next set of proposals for the JavaScript language. You can find a description of the proposals here.
Please take two minutes to fill out our short survey.

Die schreibgeschützte formData-Eigenschaft des FormDataEvent-Interfaces enthält das FormData-Objekt, das die Daten darstellt, die im Formular enthalten sind, als das Ereignis ausgelöst wurde.

Wert

Ein FormData-Objekt.

Beispiele

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);
});

Spezifikationen

Specification
HTML
# the-formdataevent-interface:dom-formdataevent-formdata-2

Browser-Kompatibilität

Siehe auch