dongxi1680 2015-06-04 10:30
浏览 32
已采纳

获取复选框上所选复选框的总和值,在ajax响应后单击jquery

I need to get the sum value of selected check boxes on check box click with jquery after ajax response.

$('.check:checked').each(function() {   });

The above jQuery function works if I call the check box when the page loading but I have to call this function after an AJAX response it's not working.

So I have tried with below one but no idea to get the sum of the 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 an 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 as $m)
            {
                $sql_cls = mysql_query("SELECT book FROM cls_room_tb WHERE cls_brnch_id='$br_id' AND 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) FROM other_payments WHERE br_id='$br_id' AND pay_type='$payType' AND 
                    pYear = '$pYear' AND 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>'; 
                }
            }
?>
  • 写回答

1条回答 默认 最新

  • douxu5845 2015-06-04 10:50
    关注

    Sum it up 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') for checked ones 
            // even none seems checked regarding your servser side script
            // so maybe use `.find('.check')` instead but that's not clear what you are expecting here???
            var sum = $('#ajax_response').find(':checked').map(function () {
                return +this.value
            }).get().reduce(function (a, b) {
                return a + b;
            });
            // do whatever you want with `sum`
            console.log(sum);
        }
    });
    

    Or maybe you want to call it on:

    $(document).on('change', '.check', function () {
        var sum = $('.check').filter(':checked').map(function () {
            return +this.value
        }).get().reduce(function (a, b) {
            return a + b;
        }/* to start with default value != 0 as '5' in your posted code*/, 5);
        // do whatever you want with `sum`
        console.log(sum);
    }
    

    In fact, i'm not sure to understand what is your expected behaviour. How/when do you wish to sum up checked checkboxes values?!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3