如何在 FormData 中获取多个复选框值?
How do I get multiple checkbox values in FormData?
我的 html 表单有多个具有相同 name
属性的复选框:
<form id='cheese-selector'>
<input type="checkbox" name="cheese" id="limburger" value="Limburger">Limburger</input>
<input type="checkbox" name="cheese" id="camembert" value="Camembert">Camembert</input>
<input type="checkbox" name="cheese" id="roquefort" value="Roquefort">Roquefort</input>
<input type="submit" id="pick-cheese">Pick My Cheese!</input>
</form>
我正在通过 ajax 提交。当我创建一个 FormData
对象时,只有第一个选中的复选框被包括在内,即使我已经选中了多个:
$('#limburger').click()
$('#camembert').click()
console.log(FormData(this).get('cheese'))
// 'Limburger'
如何FormData
将所有选中的值编码为数组?
见MDN:
The get() method of the FormData interface returns the first value associated with a given key from within a FormData object. If you expect multiple values and want all of them, use the getAll() method instead.
我的 html 表单有多个具有相同 name
属性的复选框:
<form id='cheese-selector'>
<input type="checkbox" name="cheese" id="limburger" value="Limburger">Limburger</input>
<input type="checkbox" name="cheese" id="camembert" value="Camembert">Camembert</input>
<input type="checkbox" name="cheese" id="roquefort" value="Roquefort">Roquefort</input>
<input type="submit" id="pick-cheese">Pick My Cheese!</input>
</form>
我正在通过 ajax 提交。当我创建一个 FormData
对象时,只有第一个选中的复选框被包括在内,即使我已经选中了多个:
$('#limburger').click()
$('#camembert').click()
console.log(FormData(this).get('cheese'))
// 'Limburger'
如何FormData
将所有选中的值编码为数组?
见MDN:
The get() method of the FormData interface returns the first value associated with a given key from within a FormData object. If you expect multiple values and want all of them, use the getAll() method instead.