html - 如果未选中复选框,则动态表将值添加到列
问题描述
如果未选中复选框的行,我需要将支付金额添加回未结金额。目前我现在面临的是当我取消检查时,未结清的金额不断减少并且没有加回来。我的第二行没有支付金额,但如果我检查它也会继续减少价值。有人可以帮助我编码吗?
初始版本
Name | Outstanding | Pay |
Item 1| 20,000 | 0.00 |
Item 2| 10,000 | 0.00 |
Item 3| 3,000 | 0.00 |
我对检查的预期结果:
Name | Outstanding | Pay | |
Item 1| 14,000 | 6,000 | checked |
Item 2| 7,000 | 3,000 | checked |
Item 3| 3,000 | 0.00 | |
我对未选中的预期结果:
如果未选中,它将添加回未结金额。
Name | Outstanding | Pay | |
Item 1| 20,000 | 6,000 | unchecked|
Item 2| 7,000 | 3,000 | checked |
Item 3| 3,000 | 0.00 | |
我遇到的问题:
- 我
6,000
在第一行输入并检查,未完成的将变为14,000
. 然后我检查了没有输入支付金额的第二行,第二行未付金额也会扣除并再次扣除第一行。但由于支付金额为零,因此不应再次扣除或扣除金额。 - 如果我一直不检查和检查,金额将不断减少。
Name | Outstanding | Pay | |
Item 1| 8,000 | 6,000 | checked |
Item 2| 18,000 | 0.00 | checked |
Item 3| 3,000 | 0.00 | |
$(function() {
function calcSum(t) {
var result = 0.00;
var OutAmt = 0.00;
var Amt = 0.00;
$("tbody tr").each(function(i, r) {
if ($('input[name="chck"]', r).is(":checked")) {
result += parseFloat($(".payAmt", r).val());
OutAmt += parseFloat($(".outstandingAmt", r).text().replace(/,/g, ''))
Amt = $('.outstandingAmt', r).text((OutAmt - result).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,'));
}
});
return result;
}
function updateSum(tbl) {
var t = calcSum(tbl);
$("#txtUnappliedAmt").val(t.toFixed(2));
}
updateSum($("#price-list"));
$("#price-list input").change(function() {
updateSum($("#price-list"));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="price-list">
<thead>
<tr>
<th>Name</th>
<th>Outstanding</th>
<th>Pay</th>
</tr>
</thead>
<tbody>
<tr>
<td class="item name">Item 1</td>
<td class="outstandingAmt">20,000.00</td>
<td class="item price"><input class="payAmt" type="text" value="0.00"></td>
<td><input type="checkbox" name="chck"></td>
</tr>
<tr>
<td class="item name">Item 2</td>
<td class="outstandingAmt">10,000.00</td>
<td class="item price"><input class="payAmt" type="text" value="0.00"></td>
<td><input type="checkbox" name="chck"></td>
</tr>
<tr>
<td class="item name">Item 3</td>
<td class="outstandingAmt">5,000.00</td>
<td class="item price"><input class="payAmt" type="text" value="0.00"></td>
<td><input type="checkbox" name="chck"></td>
</tr>
</tbody>
</table>
<div>
<label><b>Unapplied Amount:</b></label>
<input type="text" id="txtUnappliedAmt" value="0.00" disabled>
</div>
解决方案
您可以只计算复选框已选中/未选中的行,并保留您的calcSum
函数仅用于从选中的复选框中获取总数。
演示代码:
$(function() {
function calcSum(t) {
var result = 0.00;
$("tbody tr").each(function(i, r) {
if ($('input[name="chck"]', r).is(":checked")) {
result += parseFloat($(".payAmt", r).val());//just for total
}
});
return result;
}
function updateSum(tbl) {
var t = calcSum(tbl);
$("#txtUnappliedAmt").val(t.toFixed(2));
}
updateSum($("#price-list"));
//keep this on check of checkbox..
$("#price-list input:checkbox").change(function() {
var closest_row = $(this).closest("tr")//closest row..
var pay_ = parseFloat($(".payAmt", closest_row).val())
var OutAmt = parseFloat($(".outstandingAmt", closest_row).text().replace(/,/g, ''))
if ($(this).is(":checked")) {
$('.outstandingAmt', closest_row).text((OutAmt - pay_).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')); //deduce
} else {
$('.outstandingAmt', closest_row).text((OutAmt + pay_).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')); //add
}
updateSum($("#price-list"));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="price-list">
<thead>
<tr>
<th>Name</th>
<th>Outstanding</th>
<th>Pay</th>
</tr>
</thead>
<tbody>
<tr>
<td class="item name">Item 1</td>
<td class="outstandingAmt">20,000.00</td>
<td class="item price"><input class="payAmt" type="text" value="0.00"></td>
<td><input type="checkbox" name="chck"></td>
</tr>
<tr>
<td class="item name">Item 2</td>
<td class="outstandingAmt">10,000.00</td>
<td class="item price"><input class="payAmt" type="text" value="0.00"></td>
<td><input type="checkbox" name="chck"></td>
</tr>
<tr>
<td class="item name">Item 3</td>
<td class="outstandingAmt">5,000.00</td>
<td class="item price"><input class="payAmt" type="text" value="0.00"></td>
<td><input type="checkbox" name="chck"></td>
</tr>
</tbody>
</table>
<div>
<label><b>Unapplied Amount:</b></label>
<input type="text" id="txtUnappliedAmt" value="0.00" disabled>
</div>
推荐阅读
- html - 使html中的左卡作为按钮反应以获取模态窗口
- javascript - ExtJS,如何将 ViewModel 绑定到组件配置参数?
- python - Tkinter:使用鼠标绘制矩形,并以该矩形的形状裁剪照片
- mysql - 如何在 sql 中使用 group_concat() 获取不同的值
- php - wp-cli 错误“/usr/bin/env:«mysql»:不存在 el archivo o el directory”
- vba - 使用 vba 语句或代码更新文本文件内容
- c++ - 为什么我输出如下示例时会出现错误?
- javascript - jQuery滚动到另一个页面上的部分
- javascript - 当下一个 js 发生错误时,Typescript 编译器不会停止
- python - MacOS 与 Windows 上的 Tkinter 网格列间距是否存在固有差异?如果是这样,我该如何解决?