javascript - 获取表格每一行的复选框总和
问题描述
我想获取表格中每一行的复选框的总和
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);
});
});
});
现在唯一的问题是上面的代码没有对行值求和,当我单击全选时。对于这个问题有什么帮助吗?
提前致谢
解决方案
$(this).parents("tr").children("td").find('input[type=checkbox]:checked')
当您勾选checkall
复选框时,它将被此选择器匹配。它没有明确的value
集合,因此.val()
将返回 string "on"
。
显然,字符串"on"
不能转换为整数,所以你total
会变成NaN
.
从您的选择器中排除.checkall
复选框,您的代码将起作用。
$(this).parents('tr').find(':checkbox:checked').not(".checkall").each(...
注意:您还应该注意jQuery 文档-$(document).ready(fn)
自 v3 以来已被弃用。改为使用$(fn)
。
编辑:根据您在CodeProject上发布的更新的 Fiddle,您只需在从函数更新它后触发您的复选框事件:change
SelectAll
table.find('td:nth-child(' + columnIndex + ') input').prop("checked", obj.checked).change();
推荐阅读
- java - 只能使用 JDBC Sql Server 连接到主数据库
- office-js - 如何使用office.js在excel中删除工作表中的工作表行
- entity-framework - 从 DTO 批量更新实体
- alibaba-cloud - 使用 OSS Utility 工具在阿里云 OSS 的特定区域创建存储桶?
- javascript - 如何为带有“document.write”的javascript函数编写单元测试用例
- scala - spark scala 在日期列和时间列中拆分时间戳列
- javascript - 在这个 Promise 实现示例中,为什么要检查解析器是否有 .then?
- python-3.x - 为什么这个while循环中的语句序列会改变输出
- javascript - 有什么办法可以避免谷歌图表中继器
- bash - 如何在 Windows 10 上的 msys bash.exe 中抑制“msys\r\n”的打印