2016-08-23 3 views
-2

私は、(128879)から(128,879)のような表の値を書式設定する作業中のプロジェクトで作業しています。私がこれまで使用してきたコードは次のとおりです。何千もの区切りのカンマを含む数値の書式設定

$(document).ready(function(){ 
$('#tableOne tr td:nth-child(3)').each(function(){ 
    var num = $(this).text; 
    num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,") 
}); }); 

また、私はjQueryのNumberFormatterライブラリを試してみましたが、エラーコードはjQueryのは、(Chromeを使用してデバッガのページ上のコードの最後の行)で定義されていないことを示すと数学.js(運がない)。なぜこれが起こっているのか分かりません。 jQueryとMath.jsに使用

<script>タグを以下に示します。

<script type = 'text/javascript' src ="jquery.numberformatter-1.2.4.min.js"></script> 
<script type="text/javascript" src="math.js"></script> 
+0

'num.toString()。replace(' ... ');'それ自体は実際には何も更新しません。これはおそらく '$(this).text = num.toString()。replace(' ... ');'です。 – Xufox

+0

[Replace method does not work]の重複の可能性があります(http://stackoverflow.com/questions/1433212/replace-method-doesnt-work) – Xufox

答えて

0

あなたが望むようにあなたが$を使用できるようにjQueryライブラリを含める必要があります。また、$(this).textは関数です。 num = $(this).text()$(this).text(num.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"))が必要です。私はあなたの正規表現をテストしていないので、動作するかどうかわかりません。

関連する問題