Javascript 购物车页面价格计算问题,逗号不显示总价$NaN

Javascript 购物车页面价格计算问题,逗号不显示总价$NaN,javascript,jquery,Javascript,Jquery,我正在计算购物车页面上的价格,请在购物车上添加两个产品并检查。如果价格低于999,如果价格像1000,那么不显示$NaN的总价格就是我的代码 <script> function increment_quantity(product_id, price) { var inputQuantityElement = $("#quantity-"+product_id); var newQuantity = parseInt($(inputQuantity

我正在计算购物车页面上的价格,请在购物车上添加两个产品并检查。如果价格低于999,如果价格像1000,那么不显示$NaN的总价格就是我的代码

<script>
function increment_quantity(product_id, price) {
    var inputQuantityElement = $("#quantity-"+product_id);
    var newQuantity = parseInt($(inputQuantityElement).val())+1;
    var newPrice = newQuantity * price;
    save_to_db(product_id, newQuantity, newPrice);
}
function decrement_quantity(product_id, price) {
    var inputQuantityElement = $("#quantity-"+product_id);
    if($(inputQuantityElement).val() > 1) 
    {
    var newQuantity = parseInt($(inputQuantityElement).val()) - 1;
    var newPrice = newQuantity * price;
    save_to_db(product_id, newQuantity, newPrice);
    }
}
function numberWithCommas(number) {
    var parts = number.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return parts.join(".");
}
function save_to_db(product_id, new_quantity, newPrice) {
    var inputQuantityElement = $("#quantity-"+product_id);
    var priceElement = $("#cart-price-"+product_id);
    var form_data = new FormData();
    form_data.append('action', 'cart-qty');
    form_data.append('product_id', product_id);
    form_data.append('new_quantity', new_quantity);
    form_data.append('new_price', newPrice);
    $.ajax({
        url: 'load.php',
        dataType: 'json',
        processData: false,
        contentType: false,
        data: form_data,
        type: 'post',
        success : function(response) {
            $(inputQuantityElement).val(new_quantity);
            $(priceElement).attr('data-price', newPrice);
            $(priceElement).text("$ "+numberWithCommas(Number(newPrice.toFixed(2))));
            var totalQuantity = 0;
            $("input[id*='quantity-']").each(function() {
                var cart_quantity = $(this).val();
                totalQuantity = parseInt(totalQuantity) + parseInt(cart_quantity);
            });
            $("#ttl-qty").text(totalQuantity);
            var totalItemPrice = 0;
            $("p[id*='cart-price-']").each(function() {
                var cart_price = $(this).attr('data-price').replace("$","");
                totalItemPrice = Number(totalItemPrice) + Number(cart_price);
            });
            $("#sttlprc").text("$ "+numberWithCommas(totalItemPrice.toFixed(2)));
            $("#ttlprc").text("$ "+numberWithCommas(totalItemPrice.toFixed(2)));
        }
    });
}
</script>

功能增量数量(产品id、价格){
var inputQuantityElement=$(“#数量-”+产品id);
var newQuantity=parseInt($(inputQuantityElement).val())+1;
var newPrice=新数量*价格;
保存到数据库(产品id、新数量、新价格);
}
功能递减量(产品id、价格){
var inputQuantityElement=$(“#数量-”+产品id);
if($(inputQuantityElement).val()>1)
{
var newQuantity=parseInt($(inputQuantityElement).val())-1;
var newPrice=新数量*价格;
保存到数据库(产品id、新数量、新价格);
}
}
带逗号的函数编号(编号){
var parts=number.toString().split(“.”);
零件[0]=零件[0]。替换(/\B(?=(\d{3})+(?!\d))/g,“,”;
返回零件。连接(“.”);
}
功能保存到数据库(产品id、新数量、新价格){
var inputQuantityElement=$(“#数量-”+产品id);
var priceElement=$(“#购物车价格-”+产品id);
var form_data=new FormData();
表单_data.append('action','cart qty');
表单\数据。追加('product\ U id',product\ U id');
表格数据。追加(“新数量”,新数量);
表格数据。追加(‘新价格’,新价格);
$.ajax({
url:'load.php',
数据类型:“json”,
processData:false,
contentType:false,
数据:表格数据,
键入:“post”,
成功:功能(响应){
$(inputQuantityElement).val(新数量);
$(priceElement).attr('data-price',newPrice);
$(priceElement).text(“$”+带逗号的数字(数字(newPrice.toFixed(2)));
var totalQuantity=0;
$(“输入[id*='quantity-']”)。每个(函数(){
var cart_quantity=$(this).val();
totalQuantity=parseInt(totalQuantity)+parseInt(购物车数量);
});
$(“ttl数量”).text(总数量);
var totalItemPrice=0;
$(“p[id*='cart-price-'])。每个(函数(){
var cart_price=$(this.attr('data-price')。替换(“$”,”);
totalItemPrice=编号(totalItemPrice)+编号(购物车价格);
});
$(“#sttlprc”).text(“$”+带逗号的数字(totalItemPrice.toFixed(2));
$(“#ttlprc”).text(“$”+带逗号的数字(totalItemPrice.toFixed(2));
}
});
}

代码更新了

我刚刚修复了它,我只取了数据属性中的price值,没有$and format,现在运行正常。谢谢你的帮助sawan。

你能在JSFIDLE或codepen上发布你的代码吗?我在这里添加了它,很抱歉我第一次使用codepen,但我看到脚本不起作用。谢谢sawan,我刚刚自己解决了这个问题。另外,use可以使用$(“p[id*='cart-price-”)。每个(函数(){var cart\u price=$(this.text()。替换(“$”,”).replace(“,”);//添加replace(“,”,“”)totalItemPrice=Number(totalItemPrice)+Number(cart_price);};