计算折扣输入后的总价 OnChange无法正常工作
function CalcDiscount()
{
var qty = document.getElementById("ticket-count").innerText;
var value = document.getElementById("item-price").innerText;
var discount = document.getElementById("discount").value;
var total = value * qty;
var gtotal = total - discount;
//document.getElementById("total-price").value = gtotal;
$("#total-price").val(gtotal);
}

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
Ticket Fare: <span id="item-price" class="item-price">450</span><br />
Ticket Number:<span class="ticket-count" id="ticket-count" > 5</span><br />
Discount: <input class="discount" id="discount" name="discount" type="text" onkeyup="CalcDiscount();"> <br /><br />
Total: <input class="total-price" id="total-price" name="totalprice" type="text" >
&#13;
答案 0 :(得分:1)
您需要将值从字符串转换为数字。
试试这个:
function CalcDiscount()
{
var qty = parseInt(document.getElementById("ticket-count").innerText);
var value = parseInt(document.getElementById("item-price").innerText);
var discount = parseInt(document.getElementById("discount").value);
var total = value * qty;
var gtotal = total - discount;
//document.getElementById("total-price").value = gtotal;
$("#total-price").val(gtotal);
}