FormData() 构造函数创建新 FormData 对象。

注意 : This feature is available in Web 工作者 .

句法

var formData = new FormData(form)
					

参数

form 可选
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 对象:

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">
  <div>
    <label for="username">Enter name:</label>
    <input type="text" id="username" name="username">
  </div>
  <div>
    <label for="useracc">Enter account number:</label>
    <input type="text" id="useracc" name="useracc">
  </div>
  <div>
    <label for="userfile">Upload file:</label>
    <input type="file" id="userfile" name="userfile">
  </div>
  <input type="submit" value="Submit!">
</form>
					

注意 : 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);
					

规范

规范 状态 注释
XMLHttpRequest
The definition of 'FormData()' in that specification.
实时标准 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
FormData() 构造函数 Chrome 7 Edge 12 Firefox 4 IE 10 Opera 12 Safari 5 WebView Android ≤37 Chrome Android 18 Firefox Android 4 Opera Android 12 Safari iOS 5 Samsung Internet Android 1.0

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: