php - Get the sum value of selected check boxes on check box click with jquery after ajax response -
i need sum value of selected check boxes on check box click jquery after ajax response.
$('.check:checked').each(function() { });
the above jquery function works if call check box when page loading have call function after ajax response it's not working.
so have tried below 1 no idea sum of selected check boxes value
$('body').on('click', '.check', function(){ }
jquery function
<script> $(document).ready(function(){ $('body').on('click', '.check', function(){ var tot = 5; $('.check:checked').each(function() { console.log($(this).val()); tot += $(this).val(); }); console.log(tot); }); $('#paytype').change(function(){ var ptype = $(this).val(); var y = $('[name = "year"]').val(); clearruntimefunctions(); $('#paytype').val(ptype); if(ptype != "dntsntreqst"){ $('#ajax_img_lod').show(); $.ajax({ url: 'ajax_requst/getotherpaymt.php', type: 'post', data: { 'pyear': y, 'paytype': ptype, }, success: function( data ){ $('#ajax_img_lod').hide(); $('#ajax_response').html(data); } }); } }); }); </script>
this ajax response page
<?php <table class="table table-message" style="margin: 10px 25px;max-width: 350px;"> <tbody> <tr class="heading"> <td class="cell-title"> year </td> <td class="cell-title"> month </td> <td class="cell-title"><div align="right"> amount </div></td> <td class="cell-title"><div align="right"> balance</div> </td> <td class="" width="2%"><div align="right"></div></td> </tr> foreach($monthend_arry $m) { $sql_cls = mysql_query("select book cls_room_tb cls_brnch_id='$br_id' , start_date <= '$m'"); $nocls = mysql_num_rows($sql_cls); if ($nocls > 0) { if ($nocls <= 4) { $centfee = $nocls * 1000; } else { $centfee = 4 * 1000; $centfee += ($nocls - 4) * 500; } $sql_paid = mysql_query("select sum(amount) other_payments br_id='$br_id' , pay_type='$paytype' , pyear = '$pyear' , pmonth='". substr($m , 5, 2)."'"); $res_paid = mysql_fetch_row($sql_paid); $paidamount = $res_paid[0]; $amount = $centfee ; echo '<tr class="unread"> <td class="cell-title" >'.$pyear.'</td> <td class="cell-title" >'.month_crt( substr($m , 5, 2)).' - '.$nocls.' </td> <td class="cell-title" > <div align="right"> '.numformt($amount).'</div> </td> <td class="cell-title" ><div align="right">'.numformt( $amount - $paidamount ).'</div></td> <td class="cell-title" > <input type="checkbox" class="check" name="checkpay[]" value="'.numformt( $amount - $paidamount ).'" /> </td> </tr>'; } } ?>
sum inside ajax success callback, e.g:
$.ajax({ url: 'ajax_requst/getotherpaymt.php', type: 'post', data: { 'pyear': y, 'paytype': ptype, }, success: function (data) { $('#ajax_img_lod').hide(); $('#ajax_response').html(data); // find(':checked') checked ones // none seems checked regarding servser side script // maybe use `.find('.check')` instead that's not clear expecting here??? var sum = $('#ajax_response').find(':checked').map(function () { return +this.value }).get().reduce(function (a, b) { return + b; }); // whatever want `sum` console.log(sum); } });
or maybe want call on:
$(document).on('change', '.check', function () { var sum = $('.check').filter(':checked').map(function () { return +this.value }).get().reduce(function (a, b) { return + b; }/* start default value != 0 '5' in posted code*/, 5); // whatever want `sum` console.log(sum); }
in fact, i'm not sure understand expected behaviour. how/when wish sum checked checkboxes values?!
Comments
Post a Comment