简体   繁体   中英

im using onkeyup() to call javascript function which adds some value, while using backspace it not subtracts the value instead it adds every values

im using the below function in onkeyup() event of a textbox to add two values of textbox, while entering every digits of numbers in textbox it adds also while deleting number digits it adds the remaining digits, . i wants to decrement the sum value while deleting digits using backspace.

function round_off(){
var inv=document.getElementById("invoice").value ;
var rnd_off=document.getElementById("round_off").value ;
var invo=parseFloat(inv) + parseFloat(rnd_off);
invoice.value=invo.toFixed(2);
}; 

thanks in advance

I'm not sure what you really need, but I guess you can start with this:

function round_off (e) {
    var inv = +document.getElementById('invoice').value,
        rnd_off = +document.getElementById('round_off').value,
        invo;
    if (e.keyCode === 8) { // 8 = Backspace
        invo = inv - rnd_off;
    } else {
        invo = inv + rnd_off;
    }
    invoice.value = invo.toFixed(2);
    return;
}

Notice also, that you'll need to pass event to the handler:

... onkeyup="round_off(event)" ...

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