Monday, 15 April 2013

javascript - calculate TOTAL CONSUMPTION and Consumption per person -



javascript - calculate TOTAL CONSUMPTION and Consumption per person -

i seek calculate calculate total consumption , consumption per person.

relevant code :

$(function () { $(".items").keyup(function (event) { var total = 0; $(".items").each(function () { var gtotal = 0; $(this).find(".targetfields").each(function () { var qty = parseint($(this).find(".quantity").val()||'0'); var rate = parseint($(this).find(".rate").val()||'0'); var subtotal = qty * rate; $(this).find(".subtotal").val(subtotal); if (!isnan(subtotal)) gtotal += subtotal; }); $(this).find(".g_total").html(gtotal + "din"); total += gtotal; }); $("#total").html(total + "din"); }); });

a fiddle tests : http://jsfiddle.net/odisej/cgehu/

i stuck...what's wrong in script?

parseint('') gives nan. , when multiply nan number, nan.

so i'd suggest remplace

var qty = parseint($(this).find(".quantity").val()); var rate = parseint($(this).find(".rate").val());

with

var qty = parseint($(this).find(".quantity").val())||0; var rate = parseint($(this).find(".rate").val())||0;

javascript

No comments:

Post a Comment