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



我正在计算购物车页面上的价格http://199.192.21.232/~admin/cart.php请在购物车上添加两个产品并进行检查。如果价格低于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>

代码更新

我刚刚修复了它,我只是在没有$和格式的数据属性中获取价格的值,现在它工作得很好。谢谢你的帮助,萨万。

最新更新