0

以下在 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>&nbsp;</td>
    <td>&nbsp;</td>

 <td>Total Cost Half Day</td>
<input type="text" name="total_half" id="total_half"  />

<td>&nbsp;</td>
    <td>&nbsp;</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>

http://jsfiddle.net/newbie123/ue62p/5/

4

1 回答 1

1

onChange 仅在用户更新值时触发。如果您想要该功能,则必须将 calculateSubTotal 添加到更新 total_full 和 total_half 的事件中。

于 2012-06-17T01:31:06.630 回答