jquery calculation sum two different type of item

Posted by st4n on Stack Overflow See other posts from Stack Overflow or by st4n
Published on 2010-05-26T21:25:10Z Indexed on 2010/05/26 21:41 UTC
Read the original article Hit count: 254

I'm writing a script like the example shown in the demo where All of the "Total" values (Including the "Grand Total") are Automatically Calculated using the calc () method. at this link:

But I have some fields in which to apply the equation qty * price, and others where I want to do other operations .. you can tell me how? thank you very much

i try with this, but it is a very stupid code .. and the grandTotal .. not sum the two different fields:

function recalc() {
  $("[id^=total_item]").calc("qty * price", {
    qty: $("input[name^=qty_item_]"),
    price: $("[id^=price_item_]")
  }, function (s){
    // return the number as a dollar amount
    return "$" + s.toFixed(2);
  }, function ($this){
    // sum the total of the $("[id^=total_item]") selector
    var sum = $this.sum();

    $("#grandTotal").text(
      // round the results to 2 digits
      "$" + sum.toFixed(2)
    );
  });

  $("[id^=total_otheritem]").calc("qty1 / price1", {
    qty1: $("input[name^=qty_other_]"),
    price1: $("[id^=price_other_]")
  }, function (s){
    // return the number as a dollar amount
    return "$" + s.toFixed(2);
  }, function ($this){
    var sum = $this.sum();
    $("#grandTotal").text(
      // round the results to 2 digits
      "$" + sum.toFixed(2)
    );
  });
}

© Stack Overflow or respective owner

Related posts about jquery-plugins

Related posts about jquery-calculation