【发布时间】:2017-09-20 16:53:02
【问题描述】:
我有下表
<table>
<tr style="background-color: silver;">
<th>Check it</th>
<th>Estimate item</th>
<th>Quantity</th>
<th>Cost</th>
</tr>
<tr>
<td><input type="checkbox" value="450" /></td>
<td>Remove Tile</td>
<td><input type="text" value="1"></td>
<td>$450</td>
</tr>
<tr>
<td><input type="checkbox" value="550" /></td>
<td>Remove Tub</td>
<td><input type="text" value="1"></td>
<td>$550</td>
</tr>
<p>Calculated Price: $<input type="text" name="price" id="price" disabled /></p>
Table example 我找到了如何计算复选框值的总和:
<script>
$(document).ready(function () {
var $inputs = $('input[type="checkbox"]')
$inputs.on('change', function () {
var sum = 0;
$inputs.each(function() {
if(this.checked)
sum += parseInt(this.value);
});
$("#price").val(sum);
});
});
</script>
问题是: 如果用户将更新文本框中的数量,我需要更新总数。 我希望它以两种方式工作:在选中复选框之前或之后更改数量。
所以“成本”列中的值等于复选框值。我不想修改“成本”列。我需要在“带有 id="price" 文本框的文本框的表格底部显示总计。
案例: 用户选中第一个复选框#price 应更新为 450。 用户检查了第二个复选框#price 现在应该是 1000。 用户在第一个复选框的行中将数量更改为 2。现在 #price 应更新为 1450
提前致谢!
【问题讨论】:
-
或...hscripts.com/tutorials/javascript/dom/checkbox-events.php您只需点击搜索引擎即可获取此信息...以及示例。
-
这不是我想要的。我有一个值为 450 且文本框(数量)值 = 1 的复选框。总价为 450。如果我将选中第二个值为 50 的复选框,文本框(数量)将 = 1。总价将 = 500。现在如果我将复选框编号 1 的数量更改为数量 2 .总数应该是950。这是一个估算应用程序。
标签: javascript jquery checkbox