2016-12-05 50 views
0

在我的脚本中,我有一张表,其中我得到了每列的总值。 我想实现的是用逗号显示数字。用逗号格式化数字,得到总价值

这里是我迄今为止

HTML

<table> 
<thead> 
     <tr>  
      <th>COL1</th> 
      <th>COL2</th> 
      <th>COL3</th> 
     </tr> 
</thead> 


<tbody> 
<tr> 
    <td><input type="text" class="num1" name="num1" value="1200.00" /></td> 
    <td><input type="text" class="num2" name="num2" value="500.00" /></td> 
    <td><input type="text" class="num3" name="num3" value="100.00" /></td> 
</tr> 

<tr> 
    <td><input type="text" class="num1" name="num1" value="900.00" /></td> 
    <td><input type="text" class="num2" name="num2" value="1500.00" /></td> 
    <td><input type="text" class="num3" name="num3" value="10.00" /></td> 
</tr> 

<tr> 
    <td><input type="text" class="num1" name="num1" value="200.00" /></td> 
    <td><input type="text" class="num2" name="num2" value="500.00" /></td> 
    <td><input type="text" class="num3" name="num3" value="300.00" /></td> 
</tr> 


<tr> 
    <td><input type="text" class="tot1" name="tot1" value="" /></td> 
    <td><input type="text" class="tot2" name="tot2" value="" /></td> 
    <td><input type="text" class="tot3" name="tot3" value="" /></td> 
</tr> 
<tr> 

</tr> 
</tbody> 
</table> 

的JavaScript

$(document).ready(function() { 
      $(".num1, .num2, .num3").each(function() { 
       $(this).keyup(function() { 
        calculateSum(); 
       }); 
      }); 
     }); 


     function calculateSum() { 
      var tot1 = 0; 
      var tot2 = 0; 
      var tot3 = 0; 

      // Paying 

      $(".num1").each(function() { 
       if (!isNaN(this.value) && this.value.length != 0) { 
        tot1  += parseFloat(this.value); 
       } 
      }); 
      $("input[name='tot1']").val(tot1.toFixed(2)); 


      $(".num2").each(function() { 
       if (!isNaN(this.value) && this.value.length != 0) { 
        tot2+= parseFloat(this.value); 
       } 
      }); 
      $("input[name='tot2']").val(tot2.toFixed(2)); 

      $(".num3").each(function() { 
       if (!isNaN(this.value) && this.value.length != 0) { 
        tot3+= parseFloat(this.value); 
       } 
      }); 
      $("input[name='tot3']").val(tot3.toFixed(2)); 



     } 
     window.onload = calculateSum; 

这里是我的演示小提琴。

Demo here

任何帮助,将不胜感激!谢谢。

+0

你是什么意思你想用逗号显示数字? –

+1

是不是已经添加了逗号?回顾演示 –

+0

我认为它只能自动添加到jsfiddle中。 –

回答

1

您可以calculateSum()作为改变输入的值,

$(".num1").each(function() { 
    if (!isNaN(this.value) && this.value.length != 0) { 
     tot1 += parseFloat(this.value); 
     num1 = parseFloat(this.value); 
     this.value = num1.formatMoney(2, '.', ','); 
    } 
});  
$("input[name='tot1']").val(tot1.formatMoney(2, '.', ',')); 

在这个看看:

Working Fiddle

0

你只需要使用toLocaleString如下(或像this forked fiddle):

$(".num1").each(function() { 
    if (!isNaN(this.value) && this.value.length != 0) { 
     tot1  += parseFloat(this.value); 
    } 
}); 
$("input[name='tot1']").val(tot1.toLocaleString()); 


$(".num2").each(function() { 
    if (!isNaN(this.value) && this.value.length != 0) { 
     tot2+= parseFloat(this.value); 
    } 
}); 
$("input[name='tot2']").val(tot2.toLocaleString()); 

$(".num3").each(function() { 
    if (!isNaN(this.value) && this.value.length != 0) { 
     tot3+= parseFloat(this.value); 
    } 
}); 
$("input[name='tot3']").val(tot3.toLocaleString());
+0

如果你看小提琴,他有代码,即使在IE10和更早的时候也是这样! –

+0

@JaromandaX:虽然有一个相当复杂的方法,但是使用RegExps以逗号切换点 – Pineda

+0

,我没有看到toLocaleString的用法,我的观点是a)这里的代码与他的小提琴代码不匹配, b)toLocaleString只适用于IE版本11(以及所有其他优秀的浏览器) –