01-FormData对象
<body>
<script>
const fd = new FormData()
fd.append('aa', 11)
fd.append('aa', 22)
console.log(fd)
const res = fd.get('aa')
console.log(res)
const res2 = fd.getAll('aa')
console.log(res2)
</script>
</body>
02-FormData对象序列化表单
<body>
<form id="form1">
<input type="text" name="name" />
<input type="text" name="age" />
<input id="btn" type="submit" value="提交按钮" />
</form>
<script>
const btn = document.querySelector('#btn')
btn.onclick = function (e) {
e.preventDefault()
const fd = new FormData(document.querySelector('#form1'))
const name = fd.get('name').trim()
const age = fd.get('age').trim()
console.log({ name, age })
}
</script>
</body>
03-FormData遍历
<body>
<form id="form1">
<input type="text" name="name" value="sujiehao" />
<input type="text" name="age" value="18" />
<input type="text" name="gender" value="男" />
<input type="text" value="1111" />
<input id="btn" type="submit" value="提交按钮" />
</form>
<script>
const fd = new FormData(document.querySelector('#form1'))
const obj = {}
fd.forEach((value, key) => {
console.log(key, value)
obj[key] = value
})
console.log(obj);
</script>
</body>