I have a table that allows the user to add rows to it. The table has four columns. Items, Qty, Price, and Tax. The tax column is a check box. I am using the hidden input to set a value for an unchecked box. This way I can take the post data and know if I should charge tax on a specific row. (0 value means no tax, 1 value means charge tax) The problem I have is when putting check boxes in an array it will always pick up the hidden value.
Table:
<table style="width: 90%" id="myTable" class="centered-table table table-bordered">
<thead>
<tr>
<th>Item*</th>
<th>Qty*</th>
<th>Price*</th>
<th>Tax</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td style="width: 60%"><input type="text" id="detail" name="detail[]"required></td>
<td style="width: 8%"><input type="number" id="qty" name="qty[]" required></td>
<td style="width: 12%"><input type="number" id="price" name="price[]" required></td>
<input type="hidden" value="0" name="tax[]">
<td style="width: 5%"><input type="checkbox" id="tax" name="tax[]" value="1"></td>
<td style="width: 12%"><div class="inline"><input type="button" id="addButton" class="btn btn-primary btn-xs" value="Add"/></div><div class="inline"><input type="button" id="deleteButton" class="btn btn-primary btn-xs" value="Delete"/></div>
</tr>
</tbody>
For example: I have four rows. The first and last row are checked. The array looks like this [0,1,0,0,0,1]
I need the array to look like this [1,0,0,1]
Is this possible or do I need to try a completely different method of getting unchecked and checked boxes? I have tried to come up with a loop that will take away zeros but I can always find a combination of checked and unchecked boxes that will make it fail.
This is my javascript to add rows to the table
$(function(){
$("#addButton").click(function(){
$(this).closest("tr").clone(true).appendTo("#myTable");
});
$("#deleteButton").click(function(){
var x = $('#myTable tr').length;
if(x == 2){
} else {
$(this).closest("tr").remove();
}
});
});