javascript - 如何获取 FormData 中的多个复选框值?

标签 javascript jquery html form-data

我的 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/

相关文章:

jQuery 移动 : pagecreate event does not fire when navigating from page to page

Javascript - 如何检测元素宽度变化?

javascript - D3.JS 如何在鼠标悬停时计算某个属性的圆圈数

html - 打印样式表+链接

javascript - 专注于输入并使用 Jquery 移除焦点

javascript - 解析 JSON 对象数组并在 ReactJS 中显示它

PHP多行字符串与PHP

php - 在PHP/HTML网页中执行多个MySQL查询: only first query runs

javascript - 三.JS - 如何从数据URL加载 Material 纹理?

javascript - jQuery 查找的 IE7 问题?