set() 方法在 FormData interface sets a new value for an existing key inside a FormData object, or adds the key/value if it does not already exist.

差异在 set() and FormData.append is that if the specified key does already exist, set() will overwrite all existing values with the new one, whereas FormData.append will append the new value onto the end of the existing set of values.

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

句法

There are two versions of this method: a two and a three parameter version:

formData.set(name, value);
formData.set(name, value, filename);
					

参数

名称
The name of the field whose data is contained in value .
value
The field's value. This can be a USVString or Blob (including subclasses such as File ). If none of these are specified the value is converted to a string.
filename 可选
The filename reported to the server (a USVString ),当 Blob or File is passed as the second parameter. The default filename for Blob objects is "blob". The default filename for File objects is the file's filename.

注意: If you specify a Blob as the data to append to the FormData object, the filename that will be reported to the server in the "Content-Disposition" header used to vary from browser to browser.

范例

The following line creates an empty FormData 对象:

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

You can set key/value pairs on this using FormData.set :

formData.set('username', 'Chris');
formData.set('userpic', myFileInput.files[0], 'chris.jpg');
					

If the sent value is different than String or Blob it will be automatically converted to String:

formData.set('name', 72);
formData.get('name'); // "72"
					

规范

规范 状态 注释
XMLHttpRequest
The definition of 'set()' 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
set Chrome 50 Edge 18 Firefox 39 IE No Opera 37 Safari 11 WebView Android 50 Chrome Android 50 Firefox Android 39 Opera Android 37 Safari iOS 11 Samsung Internet Android 5.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: