简体   繁体   中英

How to calculate total price after discount input

Calculate total price after discount input OnChange not working

 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" > 

You need to convert your values from strings to numbers.

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM