Sunday 15 April 2012

javascript - How to auto sum input fields with currency format -


I need to input the input fields with the currency format in the total input. I can work it when only the number It is my bell:

onKeyUp Auto coma function is working correctly, but these can not add those numbers. This is my JS:

  $ (document) .ready (function () {$ ("cost"). Each (function () {$ (this) .keyup (function () { CalculateSum ()});});}); Function calculation () {var sum = 0; $ ("Cost"). Each (function () (if (IN.INAn (this.value)) and amp; this.value.length! = 0) {sum + = parseFloat (this.value);}}); $ ("#us") val (sum.toFixed (2)). } $ (Document) .ready (function () {$ ('input.cost'). Key (function (event) {// Arrow for arrow key if (event.which> = 37 & amp; amp; Event; J. & lt; = 40) {event.preventDefault ();} var $ this = $ (this); var num = $ this.val (). Replace (/, / gi, "") .split ( ""). Reverse (). Add (""); var num2 = RemoveRougeChar (num.replace (/ (.3}) / g, "$ 1,") .position (""). The following line has been simplified: (""); Console.log (num2); // The default in history is $. $ This.val (num2);});}); Function RemoveRougeChar (convert string) {if (convertstring.substring (0,1) == ",") {return convertString.substring (1, convertString.length)} return conversion string; }   

If you answer in Bela, it is appreciated.

You need to delete the comma in the number, so isNa () Recognize it as a number, therefore, change your following code:

  if (! IsNaN (this .value) & amp; amp; this.value.length! = 0) {Sum + = parseFloat (this.value); }   

For this:

  var vl = this.value.replace (',', ''); If (! INNN (VL) & amp; amp; VL.Lamp! = 0) {sum + = parseFloat (vl); }   

View

No comments:

Post a Comment