【发布时间】:2018-07-16 10:14:52
【问题描述】:
问题是当我勾选复选框时,我希望在小计之类的输入中输入第二个跨度的值,例如,第一行有两个值,例如 500 在第二行,第二行有 200,当我勾选时第一行表格复选框的值应该是 500 应该显示在带有 id subtotal 的输入中,但是当我在第二个复选框上也打勾时,值应该添加到 500 并且总值应该显示在小计 id 中,如 700,也是勾选untick 问题正在发生。
我有一个表格,其中给出了一些字段,如服务、金额、税金和行动,代码如下:
<!DOCTYPE html>
<html>
<head>
<title>table</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table cellspacing="0" rules="all" border="1" id="table1" style="border-collapse:collapse;">
<tbody>
<tr>
<th scope="col">Service </th>
<th scope="col">Amount</th>
<th scope="col">tax</th>
<th scope="col">Action</th>
</tr>
<tr>
<td>
<span>Subscription Charges</span>
</td>
<td>
<span>500.00</span>
</td>
<td>
<span>90.00</span>
</td>
<td>
<input type="checkbox" data-toggle="checkbox" class="tot_amount" value="590.00" />
</td>
</tr>
<tr>
<td>
<span>registration fees</span>
</td>
<td>
<span>200.00</span>
</td>
<td>
<span>80.00</span>
</td>
<td>
<input type="checkbox" data-toggle="checkbox" class="tot_amount" value="590.00" />
</td>
</tr>
</tbody>
</table>
<input type="text" name="subtotal" id="subtotal">
<input type="text" name="grandtotal" id="grandtotal">
<script>
$(document).ready(function() {
$(document).on("change", ".tot_amount", function() {
var total = 0.00;
if (this.checked) {
var subtotal = $(this).closest("tr").find('td').eq(1).find('span').text();
total += parseFloat($(subtotal).val());
$('#subtotal').val(total);
}
});
});
</script>
</body>
</html>
【问题讨论】:
标签: javascript jquery html