如何计算所选复选框值的总和?

时间:2015-01-08 03:46:50

标签: jquery html asp.net

我有值的复选框(值来自数据库),我想计算总费用并以总金额(在表格下方)显示。当用户选择或取消选择时,总金额将自动更新。

            <LayoutTemplate>
                 <table border="1" cellpadding="4" cellspacing="0" class="table-grid2" width="100%">
                    <tr style="background-color:#CCE1FF;">
                     <thead>
                          <td style="width:1%;" class="line_table_thL"><input type="checkbox" value="" onclick="check_chkItems_toggle(this, 'chkbox');" /></td>
                            <th class="line_table_th" style="width:1%;">No.</th>
                            <th class="line_table_th" style="width:6%;">Waybill No.</th>
                            <th class="line_table_th" style="width:4%;">Service Type</th>
                            <th class="line_table_th" style="width:5%;">Delivered/Received Date</th>
                            <th class="line_table_th" style="width:4%;">Total Fee</th>
                            <th class="line_table_th" style="width:4%;">Postal Status</th>
                            <th class="line_table_th" style="width:4%;">Register By</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr runat="server" id="itemPlaceholder">
                        </tr>
                    </tbody>
             </LayoutTemplate>
                      <ItemTemplate>
                         <td class="line_table_tdL"><input type="checkbox" name="chkbox" value="<%# Eval("postal_id") %>" /></td>
                         <td class="line_table_td"><%# Eval("RowNum") %>&nbsp;</td>
                         <td class="line_table_td"><%# Eval("WAYBILL_NO") %>&nbsp;</td>
                         <td class="line_table_td"><%# Eval("POSTAL_TYPE") %>&nbsp;</td>
                         <td class="line_table_td"><%# Eval("SR_DATE") %>&nbsp;</td>
                         <td class="line_table_td"><%# Eval("TOTAL_FEE") %>&nbsp;</td>
                         <td class="line_table_td"><%# Eval("STATUS") %>&nbsp;</td>
                         <td class="line_table_td"><%# Eval("REG_ID") %>&nbsp;</td>
                     </tr>
                      </ItemTemplate>

&#13;
&#13;
 <td>Total Amount : <span id="total"> </span></td>
&#13;
&#13;
&#13;

我的jquery:

&#13;
&#13;
 function calculate(item) {
             var total = 0;
             if (item.checked) {
                 total += parseInt(item.TOTAL_FEE);
             } else {
                 total -= parseInt(item.TOTAL_FEE);
             }
             //alert(total);
             document.getElementById('Totalcost').innerHTML = total + " /-";
         }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您可以使用:

 $('.table-grid2 input').change(function(){
     var total = 0;
     $('.table-grid2 input').each(function() {
         if (this.checked)
         {
              total += parseInt(this.value);
         }
     });
     $('#total').html(total);
 });