How Jquery get textbox array value when checkbox value checked? -
i want textbox value checked checkbox, textbox , checkbox database array.
<tr><?php foreach($arrbook $book) { ?> <td><input type="checkbox" name="book" class="book" value="<?php echo $book['id_book']; ?>"/> <?php echo $book['name_book'];?></td> <td><input type="text" name="bookprice"class="bookprice" value="<?php echo $book['book_price']; ?>"/</td> <?php } ?></tr> <td><div class="button"><a href="#" onclick="submitbookdata();">save !</a></div></td></tr>
js
<script> function submitbookdata() { var bookidarr = []; var bookpricearr = []; $('.book').each(function() { if (this.checked) { var current = $(this).val(); $(this).parents("tr").find(".bookprice").each(function)(index, n) { if ($.each('n').val() == current) { bookpricearr.push(this.value); } }); } bookidarr.push($(this).val()); }); alert(bookpricearr); } </script>
new question, same old question, questions is, in array php code, try divide 3 cells, , when try textbox value, value textbox in same row same value. problem code:
<?php $cell_index = 1; foreach($arrbook $book) { if ($cell_index == 1) { <tr> } <td> <td><input type="checkbox" name="book" class="book" value="<?php echo $book['id_book']; ?>"/> <?php echo $book['name_book'];?></td> <td><input type="text" name="bookprice"class="bookprice" value="<?php echo $book['book_price']; ?>"/</td> <?php echo "\n";?> <?php if ( ++$cell_index > 3 ) { ?> </tr> <?php echo "\n"; $cell_index = 1; } ?> <?php } ?>
and jquery same last jquery suggestion guru. , suggestion jquery process? thanks.
the html code maybe mate:
<table> <tr> <th>column 1</th> <th>column 2</th> <th>column 3</th> <th>column 4</th> </tr> <tr> <td><input type="checkbox" name="book" class="book" value="checkbox 1"/></td> <td><input type="text" name="bookprice"class="bookprice" value="200"/></td> <td><input type="checkbox" name="book" class="book" value="checkbox 2"/></td> <td><input type="text" name="bookprice"class="bookprice" value="200"/></td> <td><input type="checkbox" name="book" class="book" value="checkbox 3"/></td> <td><input type="text" name="bookprice"class="bookprice" value="200"/></td> </tr> <tr> <td><input type="checkbox" name="book" class="book" value="checkbox 4"/></td> <td><input type="text" name="bookprice"class="bookprice" value="300"/></td> <td><input type="checkbox" name="book" class="book" value="checkbox 5"/></td> <td><input type="text" name="bookprice"class="bookprice" value="200"/></td> <td><input type="checkbox" name="book" class="book" value="checkbox 6"/></td> <td><input type="text" name="bookprice"class="bookprice" value="200"/></td> </tr> <tr> <td><input type="checkbox" name="book" class="book" value="checkbox 7"/></td> <td><input type="text" name="bookprice"class="bookprice" value="400"/></td> <td><input type="checkbox" name="book" class="book" value="checkbox 8"/></td> <td><input type="text" name="bookprice"class="bookprice" value="400"/></td> <td><input type="checkbox" name="book" class="book" value="checkbox 9"/></td> <td><input type="text" name="bookprice"class="bookprice" value="400"/></td> </tr> <tr> <td><div class="button"><a href="#" onclick="submitbookdata();">save !</a></div></td> </tr> </table>
you don't need iterate through each textbox
since have 1 textbox
, checkbox
in each tr
. when check checked
property of each checkbox
, if checked textbox
value associated below:
function submitbookdata() { var bookidarr = []; var bookpricearr = []; $('.book').each(function() { if ($(this).is(':checked')) {// check checked property .is var current = $(this).val(); bookpricearr.push($(this).parents("tr").find(".bookprice").val()) //get input textbox associated } bookidarr.push($(this).val()); }); alert(bookpricearr); }
update
get value of textbox
this:
bookpricearr.push($(this).parents("td").next().find(".bookprice").val())
it traverse parent td
of checkbox
, finds next element
of parent td
witch td
contains textbox
in case , finds textbox
class .bookprice
exists inside it. have careful there should not other element
in between parent td
of checkbox
, parent td
of corresponding textbox
Comments
Post a Comment