获取 table 每行选中复选框的总和

Get the Sum of checked checkbox for each row of table

我想获取 table

中每一行复选框的总和

Javascript :总和

        $('input[type="checkbox"]').change(function() 
      {
            var total = 0;
        $('#mytable tr').each(function()
      {
            total += parseInt($(this).val());    
        $(this).parents('tr').find('input[type=text]:last').val(total);
      }); 
     });

Javascript:计数

        $('input[type="checkbox"]').change(function() {
        $('#mytable tr').each(function() {
           var count = $(this).find(':checkbox:checked').length;
         $(this).find('#count').val(count);
       });
       });

我的HTML:

               <div class="container">
               <div class="row">
               <div class="col-md-2">
                <?php
                    $i=1;
                    $sql="select namefrom
    `student` where EXTRACT(YEAR FROM colRegisteredDate)= 2015";
                    $result = mysql_query($sql) or die(mysql_error()); 
                    $data=array();
                ?>
                 <table id="mytable" style="width:100%;">
                    <tr align="center" >
                        <th style="padding:2.5px;
                          width: 10%;" rowspan="2">S.NO</th>
                     <th style="padding:2.5px; 
                          width: 55%;" rowspan="2">StudentID</th>
                     <th style="padding:2.5px;" 
                          colspan="5" style="text-align:center;">subject</th>
                     <th style="padding:2.5px;" rowspan="2">Sum</th>
                        </tr>
                        <tr>
                     <th>s1 <input type="checkbox" 
                         id="selectAll" onclick="SelectAll(this)" /></th>
                     <th>s2 <input type="checkbox" 
                         id="selectAll" onclick="SelectAll(this)"/></th>
                     <th>s3 <input type="checkbox" 
                         id="selectAll"onclick="SelectAll(this)" /></th>
                     <th>s4 <input type="checkbox"
                          id="selectAll" onclick="SelectAll(this)" /></th>
                     <th>s5 <input type="checkbox" 
                          id="selectAll" onclick="SelectAll(this)"/></th>
                    </tr>
                        <li>
                           <?php
                              while ($row = mysql_fetch_array($result)){
                                
                              //$row = mysql_fetch_assoc($result); 
                              $data[]=$row['name'];
                              $number=$row['name'];
                              $_SESSION['Roll']=$number;
                              $str = implode("\n", $data);
                              $_SESSION['value']=$number;
                              $_SESSION['url']="index.php?StudentID=$number";
                              ?>
                           <?php
                              ?>
                           <tr>
                              <td><?php echo $i; ?></td>
                              <td><a href="#" data-id='
                 <?php echo $number; ?>' class="link" id="link">  
                                 <?php
                                    echo  "$number";
                                    ?>
                        </li>
                        </a></td>
                        <td><input type="checkbox" name="checkbox"
               id="a" class="sum" value="10" data-toggle="checkbox"><br></td>
                        <td><input type="checkbox" name="checkbox"
               id="b" class="sum" value="10" data-toggle="checkbox"><br></td>
                        <td><input type="checkbox" name="checkbox"
              id="c" class="sum" value="10" data-toggle="checkbox"><br></td>
                        <td><input type="checkbox" name="checkbox"
              id="d" class="sum" value="10" data-toggle="checkbox"><br></td>
                        <td><input type="checkbox" name="checkbox"
              id="e" class="sum" value="10" data-toggle="checkbox"><br></td>
                        <td><input  type="text" id="total"><br></td>
                        </tr>
                     </table>
                  </div>
                  <?php $i=$i+1; }?>
               </div>
            </div>

 

在第一张图片中,当我点击 Select All 时,它在 paper count 中显示 1,在 sum 列中显示 10。 然后,当我点击 Select All for second time paper count increased in correct way but no change in sum column.I 想知道,每个函数怎么写。 我想在 select 单独编辑时和 select 通过 Select All 时对选中的复选框值求和 选项

更新:

我得到了一半的结果:

        $(document).ready(function()
      {  
        $('input[type="checkbox"]').change(function()
      {
               var total=0;
        $(this).parents("tr").children("td").
          find('input[type=checkbox]:checked').each(function(){ 
                total +=parseInt($(this).val());
        $(this).parents('tr').find('input[type=text]:last').val(total);
      });
      });
      }); 

现在唯一的问题是上面的代码没有对行值求和,当我点击 Select All.Any 帮助解决这个问题?

Updated Fiddle

提前致谢

$(this).parents("tr").children("td").find('input[type=checkbox]:checked')

当您勾选checkall 复选框时,它将被此选择器匹配。它没有明确的 value 集,因此 .val() 将 return 字符串 "on".

显然,字符串"on"不能转换为整数,所以你的total会变成NaN

从您的选择器中排除 .checkall 复选框,您的代码将起作用。

$(this).parents('tr').find(':checkbox:checked').not(".checkall").each(...

Updated fiddle

注意: 您还应该注意 the jQuery documentation - $(document).ready(fn) 自 v3 以来已被弃用。请改用 $(fn)


编辑: 根据您的 updated Fiddle posted on CodeProject,您只需在从 SelectAll函数:

table.find('td:nth-child(' + columnIndex + ') input').prop("checked", obj.checked).change();

Fixed updated Fiddle