最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to get multiple checkbox values from html form - Stack Overflow

programmeradmin8浏览0评论

I understand that I can use jQuery (how to get multiple checkbox value using jquery) to get checkbox values when there are multiple, but my checkbox inputs are inside an html form, so those jQuery solutions aren't working because none of them get the checkbox values from within a form.

I try to extract the values from the form, but it just creates a weird radio nodelist that seems to count the number of times the name of the checkboxes appears in the doc rather than the values.

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

}
<script src=".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>

I understand that I can use jQuery (how to get multiple checkbox value using jquery) to get checkbox values when there are multiple, but my checkbox inputs are inside an html form, so those jQuery solutions aren't working because none of them get the checkbox values from within a form.

I try to extract the values from the form, but it just creates a weird radio nodelist that seems to count the number of times the name of the checkboxes appears in the doc rather than the values.

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

}
<script src="https://ajax.googleapis./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>

But I have no idea how to get the js to get the value of whats inside the form inputs instead of counting something else, and no idea how to access the values of the checked boxes

Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Apr 6, 2017 at 20:26 S FloydS Floyd 431 gold badge1 silver badge3 bronze badges 2
  • Try using .value on something if you want its value. Similar to how you did document.forms. – takendarkk Commented Apr 6, 2017 at 20:37
  • hi- thanks so much for the reply! I did try this several times- it returns undefined because of the html checkbox name (which is something I need to have to post the data). – S Floyd Commented Apr 7, 2017 at 3:22
Add a ment  | 

3 Answers 3

Reset to default 9

You can use jQuery .map() function to iterate through each checked checkbox input type. This will return an object with the selected checkboxes. Now, to get an array from the jQuery object we can use the .get() method like this:

Try this:

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./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>

checks is an array-like object representing all the elements with that name.

Loop over it like an array. Each member will have a value property representing the value and a checked (boolean) property that will tell you if it is checked or not.

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./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="yellow">
      </th>

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

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

发布评论

评论列表(0)

  1. 暂无评论