【问题标题】:how to get multiple checkbox values from html form如何从html表单中获取多个复选框值
【发布时间】:2017-04-06 20:26:59
【问题描述】:

我知道当有多个复选框时,我可以使用 jQuery (how to get multiple checkbox value using jquery) 来获取复选框值,但我的复选框输入位于 html 表单中,因此这些 jQuery 解决方案无法正常工作,因为它们都没有获得复选框值从一个表格内。

我尝试从表单中提取值,但它只是创建了一个奇怪的无线电节点列表,它似乎计算了复选框名称出现在文档中的次数而不是值。

function validateForm() {
  var checks = document.forms["TestForm"]["ParticipantSelection[]"];
  alert(checks);

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

<form name="TestForm" action="Video1.php" method="post" onsubmit="return validateForm()">

  <table>

    <tr>
      <th><img name="<?php echo $valueindicator[0];?>" src="<?php echo $all_four_images[0];?>" height="100">
      </th>

      <th><img name="<?php echo $valueindicator[1];?>" src="<?php echo $all_four_images[1];?>" height="100">
      </th>
      <th><img name="<?php echo $valueindicator[2];?>" src="<?php echo $all_four_images[2];?>" height="100">
      </th>
    </tr>

    <tr>
      <th> <input type="checkbox" name="ParticipantSelection[]" value="image1">
      </th>

      <th><input type="checkbox" name="ParticipantSelection[]" value="image2">
      </th>

      <th><input type="checkbox" name="ParticipantSelection[]" value="image3">
      </th>
    </tr>


  </table>
  <input type="hidden" name="AnswerCondition" value="BrowserCheckAnswer">


  <button type="submit">Continue</button>

</form>

但我不知道如何让 js 获取表单输入中内容的值而不是计算其他内容,也不知道如何访问复选框的值

【问题讨论】:

  • 如果您想要它的价值,请尝试使用.value。与您的 document.forms 类似。
  • 嗨- 非常感谢您的回复!我确实尝试了几次 - 它返回 undefined 因为 html 复选框名称(这是我需要发布数据的东西)。

标签: javascript html forms checkbox nodelist


【解决方案1】:

您可以使用 jQuery .map() 函数遍历每个检查的 checkbox 输入类型。这将返回一个带有selected 复选框的对象。现在,要从 jQuery 对象中获取一个数组,我们可以像这样使用.get() 方法:

试试这个:

var validateForm = function() {
  var checks = $('input[type="checkbox"]:checked').map(function() {
    return $(this).val();
  }).get()
  console.log(checks);
  return false;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="TestForm" action="Video1.php" method="post" onsubmit="return validateForm()">
  <table>
    <tr>
      <th> <input type="checkbox" name="ParticipantSelection[]" value="image1">
      </th>

      <th><input type="checkbox" name="ParticipantSelection[]" value="image2">
      </th>

      <th><input type="checkbox" name="ParticipantSelection[]" value="image3">
      </th>
    </tr>
  </table>
  <input type="hidden" name="AnswerCondition" value="BrowserCheckAnswer"/>
  <button type="submit">Continue</button>
</form>

【讨论】:

  • 您可以将此答案标记为有用,它对您有用,因此也可以帮助其他人。谢谢
  • 嗨!它肯定计算了选择,我很兴奋!但是,检查结果是一个 Object 对象,但我可以使用 Object.values(checks) 将其取出 :) 它确实返回了一个非常奇怪的列表,尽管其中包含除了复选框值之外的一堆东西,这很难例如,计算里面有什么。
  • 更新了从 jQuery 对象中获取选定值数组的答案 :)
【解决方案2】:

checks 是一个类似数组的对象,表示具有该名称的所有元素。

像数组一样循环遍历它。每个成员都有一个代表值的value 属性和一个checked(布尔值)属性,它会告诉你它是否被选中。

【讨论】:

  • 您好,感谢您的快速回复!很抱歉没有澄清 - 我在发布之前尝试了几次。不知道为什么它不起作用,但我尝试过的这个循环的化身都没有起作用。
  • @SFloyd — 我不知道为什么它们也不起作用。尝试提供minimal reproducible example
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 2015-05-20
  • 1970-01-01
相关资源
最近更新 更多