我希望字段“sub_total”随着各种字段的更改而更新,而不是在更新一个特定字段时更新(即:“add_on”)
我尝试将 $(document).ready(function(){ 添加到代码顶部,但如果我添加它,它根本不会计算。
HTML:
<td>Total Cost Full Day</td>
<input type="text" name="total_full" id="total_full" />
<td> </td>
<td> </td>
<td>Total Cost Half Day</td>
<input type="text" name="total_half" id="total_half" />
<td> </td>
<td> </td>
<td>Addon Charge</td>
<input type="text" name="add_on" id="add_on" />
<td> </td>
<td> </td>
<td>Sub Total</td>
<input name="sub_total" type="text" id="sub_total" />
JAVASCRIPT
//Calculate Sub Total
function calculateSubTotal() {
var SubTotal = +total_full.value + +total_half.value + +add_on.value;
document.getElementById("sub_total").value = isNaN(SubTotal) ? 0 : SubTotal;
}
document.getElementById("add_on").onchange = calculateSubTotal;
document.getElementById("add_on").onkeyup = calculateSubTotal;