Jquery / javascript只有在同一行的复选框被选中时才会求和字段
jquery / javascript to sum field only if checkbox on same row is checked
我有一个jquery/javascript函数,按我的顺序计算立方体的总数。
function calculateTotalVolume() {
var grandTotalCubes = 0;
$("table.authors-list").find('input[name^="cubicvolume"]').each(function () {
grandTotalCubes += +$(this).val();
});
$("#grandtotalcubes").text(grandTotalCubes.toFixed(2));
}
上面提到的效果很好。我需要第二个函数来合计相同的字段,但只有当一个名为treating的复选框被选中。每行都有一个名为"已处理"的复选框,但由于表是动态生成的,每次都会在名称后面附加一个计数器,因此我使用name^="treated"
我在下面的东西,但这不起作用:
function calculateTotalTreatedVolume() {
var grandTotaltreatedCubes = 0;
$("table.authors-list").find('input[name^="cubicvolume"]').each(function () {
if($("table.authors-list").find('checkbox[name^="treated"]').checked){
alert('10');
grandTotaltreatedCubes += +$(this).val();
}
});
$("#grandtotaltreatedcubes").text(grandTotaltreatedCubes.toFixed(2));
}
一如既往地感谢您的帮助。
渲染的HTML输出[1动态行添加]:(仍在开发中,所以非常粗糙,请原谅)
<table class="authors-list" border=1>
<thead>
<tr>
<td></td><td>Product</td><td>Price/Cube</td><td>Qty</td><td>line total cost</td><td>Discount</td><td>Cubes per bundle</td><td>pcs per bundle</td><td>cubic vol</td><td>Bundles</td><td><input type="checkbox" class="checkall"> Treated</td>
</tr>
</thead>
<tbody>
<tr>
<td><a class="deleteRow"> <img src="http://devryan.tekwani.co.za/application/assets/images/delete2.png" /></a></td>
<td><input type="text" id="product" name="product" />
<input type="hidden" id="price" name="price" readonly="readonly"/></td>
<td><input type="text" id="adjustedprice" name="adjustedprice" /></td>
<td><input type="text" id="qty" name="qty" /></td>
<td><input type="text" id="linetotal" name="linetotal" readonly="readonly"/></td>
<td><input type="text" id="discount" name="discount" /></td>
<td>
<input type="text" id="cubesperbundle" name="cubesperbundle" >
</td>
<td>
<input type="text" id="pcsperbundle" name="pcsperbundle" >
</td>
<td>
<input type="text" id="cubicvolume" name="cubicvolume" size='5' disabled>
</td>
<td><input type="text" id="totalbundles" name="totalbundles" size='5' disabled ></td>
<td valign="top" ><input type="checkbox" id="treated" name="treated" ></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="15"><input type="button" id="addrow" value="Add Product" /></td>
</tr>
<tr>
<td colspan="3">Grand Total: R<span id="grandtotal"></span></td>
<td colspan="2">Ave Discount: <span id="avediscount"></span>%</td>
<td colspan="1">Total Cubes: <span id="grandtotalcubes"></span></td>
<td colspan="15">Treated Cubes: <span id="grandtotaltreatedcubes"></span></td>
</tr>
<tr>
<td colspan="15"><textarea rows="1" cols="50" placeholder="Specific Comments"></textarea><textarea rows="1" cols="20" placeholder="Customer Reference"></textarea>
</td>
</tr>
</tfoot>
</table>
首先去父tr,然后使用查找查找复选框在当前行,也使用检查与DOM对象不是jQuery对象,你可以使用索引器转换jQuery对象到DOM对象
改变if($("table.authors-list").find('checkbox[name^="treated"]').checked){
if($(this).closest('tr').find('checkbox[name^="treated"]')[0].checked){
checked
是实际DOM元素的属性,您所拥有的是一个jQuery元素。你需要改变这个:
$("table.authors-list").find('checkbox[name^="treated"]').checked
:
$("table.authors-list").find('checkbox[name^="treated"]')[0].checked
-^- // get DOM element
或更多jquery:
$("table.authors-list").find('checkbox[name^="treated"]').is(':checked')
您可以使用$("table.authors-list").find('checkbox[name^="treated"]:checked')
遍历"checked"复选框,并使用最接近它的输入值(假设在同一行)。
假设您的表有许多行,每一行都有一个复选框和一个输入,您可以使用:
function calculateTotalTreatedVolume() {
var grandTotaltreatedCubes = 0;
// iterate through the "checked" checkboxes
$("table.authors-list").find('input[type="checkbox"][name^="treated"]:checked').each(function () {
// use the value of the input in the same row
grandTotaltreatedCubes += +$(this).closest('tr').find('input[name^="cubicvolume"]').val();
});
$("#grandtotaltreatedcubes").text(grandTotaltreatedCubes.toFixed(2));
}
试试这个:
var grandTotaltreatedCubes = 0;
// Cache the table object here for faster processing of your code..
var $table = $("table.authors-list");
$table.find('input[name^="cubicvolume"]').each(function () {
// Check if checkbox is checked or not here using is(':checked')
if ($table.find('checkbox[name^="treated"]').is(':checked')) {
grandTotaltreatedCubes += $(this).val();
}
});
$("#grandtotaltreatedcubes").text(grandTotaltreatedCubes.toFixed(2));
更改以下行
if($("table.authors-list").find('input[name^="treated"]').checked){
if($("table.authors-list").find('input[name^="treated"]').is(':checked')){
相关文章:
- Kendo UI网格复选框列字段未定义
- Rad网格编辑模式通过选中复选框时的java脚本启用禁用所需的字段验证器
- 使用javascript提交表单并从字段/单选框/复选框/文本区域获取数据
- 如何获取字段集元素中复选框的所有ID
- 如何获取多个复选框值并分配给隐藏字段
- j查询检查复选框是否被选中,然后向输入字段添加值
- KO 绑定复选框:从代码更改“选中”属性,不更改可观察字段
- 单击“角度”复选框时更新不相关的字段
- 在克隆复选框字段后,使用jQuery获取(并更改)该字段的名称
- 如何提交表单中的所有复选框字段
- 如何将选中的属性添加到使用jquery具有某些类的复选框字段中
- 如何使用 jquery 在页面加载时检查带有值的输入复选框字段
- 更新现有数据时检索引导模式表单中的复选框字段的值时出现问题
- 尝试检测网格视图的哪一行处于编辑模式,然后在选中复选框字段时调整文本框
- JqGrid复选框字段实现失败
- Ext JS 4复选框字段未显示
- 基于复选框字段对网格视图中的行执行操作
- 在Knockout.js中,如何在映射后根据复选框字段对viewModel项进行排序
- JavaScript if语句没有在复选框字段中注册
- 给一系列克隆复选框字段一个唯一名称的困难- javascript