chuiliu/dev-notes

FormData

Closed this issue · 0 comments

创建一个FormData对象

var formdata = new FormData();

通过已有的form表单创建FormData对象

var from = document.getElementById('searchForm');
var formdata = new FormData(form);
// 获取表单值
var name = formdata.get('name');
// 添加数据
formdata.append('email', 'append@test.com');
// 添加或修改数据
formdata.set('email', 'set@test.com');
// 将数据从FormData对象上删除
formdata.delete('email');
// 判断是否存在key
formdata.has('email');  // false
formdata.has('name'); // true

遍历FormData对象

for (var item of formdata) {
    console.log(item[0], ': ', item[1]);
}

for (var item of formdata.entries()) {
    console.log(item[0], ': ', item[1]);
}

// 遍历所有的key
for (var key of formdata.keys()) {
    console.log(key);
}

// 遍历所有的value
for (var value of formdata.values()) {
    console.log(value);
}