【问题标题】:Get the Sum of checked checkbox for each row of table获取表格每一行的复选框总和
【发布时间】:2021-03-20 11:48:15
【问题描述】:

我想获取表格中每一行的复选框的总和

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>

 

在第一个图像中,当我单击全选时,它在纸张计数中显示 1,在总和列中显示 10。 然后,当我第二次单击全选时,纸张数量以正确的方式增加,但没有变化 总和列。我想知道,如何为每个函数编写。 我想在单独选择时和通过全选选择时对选中的复选框值求和 选项

更新

我得到了一半的结果:

        $(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);
      });
      });
      }); 

现在唯一的问题是上面的代码没有对行值求和,当我点击全选时。对于这个问题有什么帮助吗?

Updated Fiddle

提前致谢

【问题讨论】:

  • 值是字符串类型,你必须转换成数字值
  • 如果您创建 js fiddle 或添加有问题的 html,它将帮助人们更快地回答。

标签: javascript php jquery function checkbox


【解决方案1】:
$(this).parents("tr").children("td").find('input[type=checkbox]:checked')

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

显然,字符串"on" 无法转换为整数,因此您的total 将变为NaN

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

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

Updated fiddle

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


编辑:根据您在CodeProject 上发布的updated Fiddle,您只需从SelectAll 函数更新复选框后触发change 事件:

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

Fixed updated Fiddle

【讨论】:

  • 非常感谢您的帮助,这就是我试图做的。
猜你喜欢
  • 2015-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
  • 1970-01-01
  • 1970-01-01
  • 2014-10-19
  • 2016-06-18
相关资源
最近更新 更多