Giorgos Giorgos - 2 months ago 12
Ajax Question

Pass checkbox values into an array using ajax to jsp

I have a form like the following, I need to pass the values of checkboxes that checked I dont know how.

<input type="checkbox" id="invid" name="invid" value="100236">
<input type="checkbox" id="invid" name="invid" value="100852">
<input type="checkbox" id="invid" name="invid" value="100962">
<input type="checkbox" id="invid" name="invid" value="100965">
<input type="checkbox" id="invid" name="invid" value="101002">

<button id="submit">Submit</button>

<script>

$('#submit').click(function() {

$.ajax({
url: "collect.jsp",
type: "post",
data: Winvid: invid,
success: function(data) {
$('#response').html(data);
}
});


});
</script>

Answer

use .each , this demo check property checked

$('#submit').click(function() {
    var arrayValue = [];
   // use name or class name
   $('input[name=invid]').each(function(){
     if($(this).prop('checked')){
       arrayValue.push($(this).val())
     }
   });

  console.log(arrayValue);
  // arrayValue use in $.ajax 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="invid" value="100236">
<input type="checkbox" name="invid" value="100852">
<input type="checkbox" name="invid" value="100962">
<input type="checkbox" name="invid" value="100965">
<input type="checkbox" name="invid" value="101002">

<button id="submit">Submit</button>