Reputation: 8873
I have a following code in html, which use checkbox align as a row
<tr class="reg_name">
<th class="custom-th">Reg. Name</th>
<td class="text-center"><input type="checkbox" name="reg_name[]" value="Y:1"></td>
<td class="text-center"><input type="checkbox" name="reg_name[]" value="N:1"></td>
<td class="text-center"><input type="checkbox" name="reg_name[]" value="B:1"></td>
<td class="text-center"><input type="checkbox" name="reg_name[]" value="I:1"></td>
</tr>
with a button
<button type="button" class="purchase__save btn btn-default pull-right">Save</button>
and this js code,
$(document).ready(function() {
$('.purchase__save').click(function() {
var box_array = [];
var boxes = $('.reg_name input[name="reg_name[]"]:checked');
for (var i = 0; i < boxes.length; i++) {
box_array[i] = boxes.val();
}
console.log(box_array);
});
});
will try to get the value of checked box.
The problem, if try to check two checkboxes at the same time, the value from
this <td class="text-center"><input type="checkbox" name="reg_name[0]" value="Y:1"></td>
is the only thing I get.
How to get values of other checkboxes if they are checked?
Upvotes: 2
Views: 286
Reputation: 15565
$(document).ready(function() {
$('.purchase__save').click(function() {
var box_array = [];
var boxes = $('input:checkbox:checked');
$.each(boxes, function(i,v) {
box_array[i] = $(this).val();
})
console.log(box_array);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="reg_name">
<td class="text-center">
<input type="checkbox" name="reg_name" value="Y:1">
</td>
<td class="text-center">
<input type="checkbox" name="reg_name" value="N:1">
</td>
<td class="text-center">
<input type="checkbox" name="reg_name" value="B:1">
</td>
<td class="text-center">
<input type="checkbox" name="reg_name" value="I:1">
</td>
</tr>
</table>
<button type="button" class="purchase__save btn btn-default pull-right">Save</button>
$(this)
to get current checkbox valueUpvotes: 2
Reputation: 107
JavaScript:
$(document).ready(function() {
$('.purchase__save').click(function() {
var box_array = [];
var i = 0;
var boxes = $('.checkedIn:checked');
console.log(boxes);
$(boxes).each(function(){
box_array[i] = this.value;
i++;
});
console.log(box_array);
});
});
HTML:
<tr class="reg_name">
<th class="custom-th">Reg. Name</th>
<td class="text-center"><input type="checkbox" class="checkedIn" name="reg_name[]" value="Y:1"></td>
<td class="text-center"><input type="checkbox" class="checkedIn" name="reg_name[]" value="N:1"></td>
<td class="text-center"><input type="checkbox" class="checkedIn" name="reg_name[]" value="B:1"></td>
<td class="text-center"><input type="checkbox" class="checkedIn" name="reg_name[]" value="I:1"></td>
</tr>
<button type="button" class="purchase__save btn btn-default pull-right">Save</button>
Upvotes: 1
Reputation: 5211
Just cycle every checked input with jQuery each
function, then append the item with JavaScript push
function.
$(function() {
$(".purchase__save").click(function() {
var box_array = [];
$(".reg_name input[name='reg_name[]']:checked").each(function(i, v) {
box_array.push($(v).val());
});
console.log(box_array);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="reg_name">
<th class="custom-th">Reg. Name</th>
<td class="text-center">
<input type="checkbox" name="reg_name[]" value="Y:1">
</td>
<td class="text-center">
<input type="checkbox" name="reg_name[]" value="N:1">
</td>
<td class="text-center">
<input type="checkbox" name="reg_name[]" value="B:1">
</td>
<td class="text-center">
<input type="checkbox" name="reg_name[]" value="I:1">
</td>
</tr>
</table>
<button type="button" class="purchase__save btn btn-default pull-right">Save</button>
Upvotes: 2
Reputation: 128
Working Example
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$('.purchase__save').click(function() {
var box_array = [];
$("input:checkbox[name=reg_name]:checked").each(function(){
box_array.push($(this).val());
});
console.log(box_array);
});
});
</script>
</head>
<body>
<tr class="reg_name">
<th class="custom-th">Reg. Name</th>
<td class="text-center"><input type="checkbox" name="reg_name" value="Y:1"></td>
<td class="text-center"><input type="checkbox" name="reg_name" value="N:1"></td>
<td class="text-center"><input type="checkbox" name="reg_name" value="B:1"></td>
<td class="text-center"><input type="checkbox" name="reg_name" value="I:1"></td>
</tr>
<button type="button" class="purchase__save btn btn-default pull-right">Save</button>
</body>
</html>
Upvotes: 1
Reputation: 337713
The issue is because you're accessing the val()
of a collection of elements, this means it will only retrieve the value of the first element in the set.
You could improve this code by using map()
to build the array instead:
$('.purchase__save').click(function() {
var box_array = $('.reg_name input[name="reg_name[]"]:checked').map(function() {
return this.value;
}).get();
// use the array as required here...
});
Upvotes: 2
Reputation: 200
Because you do not have id or name specified for your inputs you need to access them from different way.
$.('.text-center > input:checked')
This should do the trick. Your searching all items with class "text-center" and theirs direct input childs with checked selector
EDIT This will return JQuery object and notice Jamiec answer!
Upvotes: -3
Reputation: 136249
you're looping over your boxes
result, but only reading the first one.
for (var i = 0; i < boxes.length; i++) {
box_array[i] = boxes.val();
}
In the above boxes
is a jquery object with 0 or more items, but boxes.val()
will just read the value of the first one.
$('.purchase__save').click(function() {
var box_array = $('.reg_name input[name="reg_name[]"]:checked').map(function(){
return $(this).val();
}).get();
console.log(box_array);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="reg_name">
<th class="custom-th">Reg. Name</th>
<td class="text-center"><input type="checkbox" name="reg_name[]" value="Y:1"></td>
<td class="text-center"><input type="checkbox" name="reg_name[]" value="N:1"></td>
<td class="text-center"><input type="checkbox" name="reg_name[]" value="B:1"></td>
<td class="text-center"><input type="checkbox" name="reg_name[]" value="I:1"></td>
</tr>
</table>
<button type="button" class="purchase__save btn btn-default pull-right">Save</button>
Upvotes: 0