Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 以货币格式添加表列_Jquery - Fatal编程技术网

Jquery 以货币格式添加表列

Jquery 以货币格式添加表列,jquery,Jquery,我用这段代码计算了一列的总数,但是计算不正确 预期产出:13500.00 目前产出:13 我认为问题出在数字的形式上,但我不知道如何处理它 <table id="sum_table" width="300" border="1"> <tr class="titlerow"> <td>Col 1</td> </tr> <tr> &

我用这段代码计算了一列的总数,但是计算不正确

预期产出:13500.00 目前产出:13

我认为问题出在数字的形式上,但我不知道如何处理它

 <table id="sum_table" width="300" border="1">
        <tr class="titlerow">
            <td>Col 1</td>
        </tr>
        <tr>
            <td class="amount">6,000.00</td>
        </tr>
        <tr>
            <td class="amount">1,500.00</td>
        </tr>
        <tr>
            <td class="amount">6,000.00</td>
        </tr>
    </table>
            <div id="total"></div>
    <script>
    var theTotal = 0;
    $(".amount").each(function () {
        theTotal += parseInt($(this).html());
    });
    $("#total").html(theTotal);
    </script>

第1列
6,000.00
1,500.00
6,000.00
var theTotal=0;
$(“.amount”)。每个(函数(){
总+=parseInt($(this.html());
});
$(“#总计”).html(总计);

您需要替换字符串中的
,以正确解析字符串

theTotal += Number($(this).html().replace(/,/g,''));
// or remove all character except digit and dot
theTotal += Number($(this).html().replace(/[^\d.]/g,''));
更新:稍后通过将总计转换为字符串将其转换为货币格式

 $("#total").html(theTotal.toString().replace(/\d(?=(?:\d{3})+$)/g,'$&,'))
 // or with 2 decimal point
 $("#total").html(theTotal.toFixed(2).replace(/\d(?=(?:\d{3})+\.)/g,'$&,'))

您的答案是正确的,但我将如何以货币格式输出它?是否可以像这样输出13500。00@ShermaineChaingan:使用toFixedGlad检查第二个以帮助您:)