简体   繁体   中英

Multiply textboxes using Javascript

I have used a jsFiddle that I found on another question on here that will work perfectly if I can get the subtotal to multiply instead of addition.

I am sure this is a really easy fix, but I'm struggling to make the change.

Can someone help me please?

Thank you in advance

http://jsfiddle.net/77uxK/48/

$(document).ready(function(){
    var sum = 0;
    function calcSum(prevVal){
        sum = sum - prevVal + (this.value/1);
        return sum;
    }
    var subAmt = $("#sub"), taxAmt = $("#tax"), totAmt = $("#total");
    $(".val").each(function(){
        var prevVal = this.value/1, self = this;
        $(this).keyup(function(){
            subAmt.val(calcSum.call(self, prevVal));
            totAmt.val(sum + sum*parseFloat(taxAmt.val()/100));
            prevVal = self.value;
        });
    });
});

just change the function to

function calcSum(prevVal) {
    var val1 = $('#val1').val();
    var val2 = $('#val2').val();
    sum = parseInt(val1) * parseInt(val2); // parseFloat based on your need change it
    return sum;
}

jsfiddle link

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