简体   繁体   中英

How to get all values of checked checkboxes in JavaScript/Node.js

I have a set of checkboxes in an html form similar to this

<form action = "/submit" method = "POST">  
  <div class="checkbox checkbox-inline checkbox-danger checkbox-md">
    <input type="checkbox" class="styled" id="val1" name="value[]" value="Apple">
    <label for="val1"> Apple </label>
  </div>
  <br>
  <div class="checkbox checkbox-inline checkbox-danger checkbox-md">
    <input type="checkbox" class="styled" id="val2" name="value[]" value="Banana">
    <label for="val2"> Banana </label>
  </div>
  <br>
  <div class="checkbox checkbox-inline checkbox-danger checkbox-md">
    <input type="checkbox" class="styled" id="val3" name="value[]" value="Pear">
    <label for="val3"> Pear </label>
  </div>
  <br>
  <div class="checkbox checkbox-inline checkbox-danger checkbox-md">
    <input type="checkbox" class="styled" id="val4" name="value[]" value="Grinadella">
    <label for="val4"> Grinadella </label>
  </div>
</form>

The form content is stored in a database. The storage works, however I can only get the value of the first checkbox and store it in the database. What I want to do is that if the user selects the first three checkboxes, I want to store the value Apple, Banana, Pear in the database separated by commas.

How can I do this. I know that in PHP there is a function called implode that does this, but how can I extend the form so that I get all values of checked checkboxes in JS/Node.js thanks in advance

You have to check the value of the check boxes

var checkBox = req.body.checkBox

if(checkBox ===true)
console.log('It is true')

I hope you get my point !!!

You can try this using map() & join() methods:

 var checkedBoxes = [...document.querySelectorAll('.styled:checked')] var values = checkedBoxes.map(c => c.value).join(', ') console.log(values)
 <div class="checkbox checkbox-inline checkbox-danger checkbox-md"> <input type="checkbox" class="styled" id="val1" name="value[]" value="Apple" checked> <label for="val1"> Apple </label> </div> <br> <div class="checkbox checkbox-inline checkbox-danger checkbox-md"> <input type="checkbox" class="styled" id="val2" name="value[]" value="Banana" checked> <label for="val2"> Banana </label> </div> <br> <div class="checkbox checkbox-inline checkbox-danger checkbox-md"> <input type="checkbox" class="styled" id="val3" name="value[]" value="Pear" checked> <label for="val3"> Pear </label> </div> <br> <div class="checkbox checkbox-inline checkbox-danger checkbox-md"> <input type="checkbox" class="styled" id="val4" name="value[]" value="Grinadella"> <label for="val4"> Grinadella </label> </div>

You can first select all the checked check boxes using querySelectorAll(':checked') . Then you can map() them to find the value. Finally join() them to get the string.

Demo:

 function getAllChecked(){ var checked = document.querySelectorAll(':checked'); var res = Array.from(checked).map(c => c.value).join(', '); console.log(res); return false; // prevent the form submission }
 <form action = "/submit" method = "POST"> <div class="checkbox checkbox-inline checkbox-danger checkbox-md"> <input type="checkbox" class="styled" id="val1" name="value[]" value="Apple"> <label for="val1"> Apple </label> </div> <br> <div class="checkbox checkbox-inline checkbox-danger checkbox-md"> <input type="checkbox" class="styled" id="val2" name="value[]" value="Banana"> <label for="val2"> Banana </label> </div> <br> <div class="checkbox checkbox-inline checkbox-danger checkbox-md"> <input type="checkbox" class="styled" id="val3" name="value[]" value="Pear"> <label for="val3"> Pear </label> </div> <br> <div class="checkbox checkbox-inline checkbox-danger checkbox-md"> <input type="checkbox" class="styled" id="val4" name="value[]" value="Grinadella"> <label for="val4"> Grinadella </label> </div> <button onclick="return getAllChecked()">Show Checked</button> </form>

Give each checkbox input a different name, then access in your post request from req.body.

HTML:

<div class="checkbox checkbox-inline checkbox-danger checkbox-md">
<input type="checkbox" class="styled" id="val1" name="value1" value="Apple" checked>
  <label for="val1"> Apple </label>
</div>
<br>
<div class="checkbox checkbox-inline checkbox-danger checkbox-md">
  <input type="checkbox" class="styled" id="val2" name="value2" value="Banana" checked>
  <label for="val2"> Banana </label>
</div>
<br>
<div class="checkbox checkbox-inline checkbox-danger checkbox-md">
  <input type="checkbox" class="styled" id="val3" name="value3" value="Pear" checked>
  <label for="val3"> Pear </label>
</div>
<br>
<div class="checkbox checkbox-inline checkbox-danger checkbox-md">
  <input type="checkbox" class="styled" id="val4" name="value4" value="Grinadella">
  <label for="val4"> Grinadella </label>
</div>

POST request:

const {
value1,
value2,
value3,
value4
       } = req.body;

Log values:

console.log(value1);
console.log(value2);
console.log(value3);
console.log(value4);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM