我的 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.
关于javascript - 如何获取 FormData 中的多个复选框值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61295995/