1. 获取提交的数据
const form = document.querySelector('form')
form.addEventListener('submit', e => {
e.preventDefault()
let formData = new FormData(form)
console.log('formData', formData);
console.log('formdata', Array.from(formData));
console.log('formdata', Array.from(formData.keys()));
console.log('uname', formData.get('uname'));/* 获取数据 */
})
2. 修改提交的数据
form.addEventListener('submit', e => {
e.preventDefault()
let formData = new FormData(form)
console.log('formdata 1', Array.from(formData));
formData.set('uname', 'glpla.github.io')/* 修改数据 */
console.log('formdata 2', Array.from(formData));
})
3. 追加数据
form.addEventListener('submit', e => {
e.preventDefault()
let formData = new FormData(form)
console.log('formdata 1', Array.from(formData));
formData.set('ugender', 'male')/* 追加数据 */
console.log('formdata 2', Array.from(formData));
})
4. 删除数据
form.addEventListener('submit', e => {
e.preventDefault()
let formData = new FormData(form)
console.log('formdata 1', Array.from(formData));
formData.delete('umsg')/* 删除数据 */
console.log('formdata 2', Array.from(formData));
})