简体   繁体   English

即时通讯使用onkeyup()来调用javascript函数,该函数会添加一些值,而在使用退格键时,它不会减去该值,而是会添加每个值

[英]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, . 我在文本框的onkeyup()事件中使用以下函数添加文本框的两个值,同时在文本框中输入数字的每个数字时,它还会添加数字,而删除数字数字时,则会添加剩余数字。 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: 还要注意,您需要将event传递给处理程序:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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