将提交的表单转换为表单数据对象

transform submited form into Form Data object

const my_form = document.getElementById('menu_filter')

my_form .addEventListener('submit', function(e) {
   e.preventDefault()
   const formData = new FormData()
   pass_submited_form(formData)
}

假设我们有这个:如何直接从表单填充 formData 对象,而不必对每个字段都使用 append() 方法?

您可以将表单本身传递给 FormData 构造函数。

let form = document.getElementById('myForm');

form.addEventListener('submit', (event) => {
  event.preventDefault();
  const formData = new FormData(form);
  
  console.log(formData.get('username'));
  console.log(formData.get('useracc'));

});
<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>

  <input type="submit" value="Submit!">
</form>