计算javascript事件onkeyup

时间:2012-06-17 01:16:43

标签: javascript

以下在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;

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

1 个答案:

答案 0 :(得分:1)

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