以下在 Jfiddle 中有效 - 但是当我以原始形式使用它时,total_full 和 total_half 字段是从其他字段自动计算的,因此没有 keyup 或 keydown 事件?因此,当“total_full”和total_half 字段更新时,“sub_total”字段不计算?
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>
JAVASCRIPT
//Calculate Sub Total
function calculateSubTotal() {
var SubTotal = +document.getElementById("total_full").value + +document.getElementById("total_half").value + +document.getElementById("add_on").value;
document.getElementById("sub_total").value = isNaN(SubTotal) ? 0 : SubTotal;
}
document.getElementById("add_on").onchange = calculateSubTotal;
document.getElementById("add_on").onkeyup = calculateSubTotal;
document.getElementById("total_full").onchange = calculateSubTotal;
document.getElementById("total_full").onkeyup = calculateSubTotal;
document.getElementById("total_half").onchange = calculateSubTotal;
document.getElementById("total_half").onkeyup = calculateSubTotal;
calculateSubTotal;
document.getElementById("total_half").onchange = calculateSubTotal;
document.getElementById("total_half").onkeyup = calculateSubTotal;
</p>