由於本文件沒有此語言的翻譯版本,您閱讀的是英文版的內容。 幫助我們翻譯這篇文章!

The FormData() constructor creates a new FormData object.

Note: This feature is available in Web Workers.


var formData = new FormData(form)


form Optional
An HTML <form> element — when specified, the FormData object will be populated with the form's current keys/values using the name property of each element for the keys and their submitted value for the values. It will also encode file input content.


The following line creates an empty FormData object:

var formData = new FormData(); // Currently empty

You could add a key/value pair to this using FormData.append:

formData.append('username', 'Chris');

Or you can specify the optional form argument when creating the FormData object, to prepopulate it with values from the specified form:

<form id="myForm" name="myForm">
    <label for="username">Enter name:</label>
    <input type="text" id="username" name="username">
    <label for="useracc">Enter account number:</label>
    <input type="text" id="useracc" name="useracc">
    <label for="userfile">Upload file:</label>
    <input type="file" id="userfile" name="userfile">
  <input type="submit" value="Submit!">

Note: Only successful form controls are included in a FormData object, i.e. those with a name, not disabled and checked (radio buttons and checkboxes) or selected (one or more options within a select).

let myForm = document.getElementById('myForm');
let formData = new FormData(myForm);


Specification Status Comment
The definition of 'FormData()' in that specification.
Living Standard Initial definition

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
FormData() constructorChrome Full support 7Edge Full support YesFirefox Full support 4IE Full support 10Opera Full support 12Safari Full support 5WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support 12Safari iOS ? Samsung Internet Android ?


Full support  
Full support
Compatibility unknown  
Compatibility unknown

See also