Javascript 获取表中每一行的复选框总数
我想获得表中每一行的总和复选框 Javascript:总的来说Javascript 获取表中每一行的复选框总数,javascript,php,jquery,function,checkbox,Javascript,Php,Jquery,Function,Checkbox,我想获得表中每一行的总和复选框 Javascript:总的来说 $('input[type="checkbox"]').change(function() { var total = 0; $('#mytable tr').each(function() { total += parseInt($(this).val()); $(this).par
$('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>
现在唯一的问题是,当我单击全选时,上面的代码没有对行值求和。对此问题有帮助吗
提前谢谢
$(this).parents("tr").children("td").find('input[type=checkbox]:checked')
勾选checkall
复选框时,此选择器将匹配该复选框。它没有显式的值设置,因此.val()
将返回“
上的字符串”
显然,
上的字符串“无法转换为整数,因此您的总数将变为NaN
从选择器中排除.checkall
复选框,代码将正常工作
$(this).parents('tr').find(':checkbox:checked').not(".checkall").each(...
NB:您还应该注意-$(文档)。ready(fn)
从v3开始就被弃用了。改用$(fn)
编辑:根据您发布的内容,您只需在通过SelectAll
功能更新复选框后,触发复选框的change
事件:
table.find('td:nth-child(' + columnIndex + ') input').prop("checked", obj.checked).change();
值是字符串类型,您必须转换为数值。如果您创建js提琴或添加有问题的html,这将帮助人们更快地回答。非常感谢您的帮助,这就是我尝试做的。
table.find('td:nth-child(' + columnIndex + ') input').prop("checked", obj.checked).change();