折扣输入后如何计算总价

时间:2016-06-15 22:11:59

标签: javascript jquery

计算折扣输入后的总价 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;
&#13;
&#13;

1 个答案:

答案 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);
}