jQuery:使用数字&;逗号
我正在开发一个更新购物车总数的功能,但我遇到了一个数字问题 当数量发生变化时,我会启动jQuery:使用数字&;逗号,jquery,integer,Jquery,Integer,我正在开发一个更新购物车总数的功能,但我遇到了一个数字问题 当数量发生变化时,我会启动updateTotals()函数,该函数将更新购物车中的总数,我遇到的问题是1000以上的数字中有一个逗号 例如,产品2是$5775,00,因此当我将数量增加1并运行updateTotals(),返回的总数是11.55,而不是11550 我对这个parseFloat(),parseInt(),Numbermalarkey非常恼火,因为无论我做了什么尝试,都无法得到正确的结果 <tr id="tr-1007
updateTotals()
函数,该函数将更新购物车中的总数
,我遇到的问题是1000以上的数字中有一个逗号
例如,产品2
是$5775,00
,因此当我将数量增加1并运行updateTotals()
,返回的总数是11.55
,而不是11550
我对这个parseFloat()
,parseInt(
),Number
malarkey非常恼火,因为无论我做了什么尝试,都无法得到正确的结果
<tr id="tr-10072229">
<td><i class="fa fa-trash text-muted remove"></i></a></td>
<td><input type="number" name="quantity" data-id="10072229" value="1" class="update-qty"></td>
<td>Product 1</td>
<td>$<span id="price-10072229">50.00</span></td>
<td>$<span id="total-10072229">50.00</span></td>
</tr>
<tr id="tr-10044516">
<td><i class="fa fa-trash text-muted remove"></i></a></td>
<td><input type="number" name="quantity" data-id="10044516" value="1" class="update-qty"></td>
<td>Product 2</td>
<td>$<span id="price-10044516">5,775.00</span></td>
<td>$<span id="total-10044516">5,775.00</span></td>
</tr>
<script>
function updateTotals(id,quantity) {
// update our price and total cols
var taxrate = 0.08;
var price = parseFloat($('#price-' + id).text().replace(',', '.'));
var ptotal = (price*quantity);
$('#total-' + id).text(ptotal.toFixed(2));
// loop through the table and get the updated totals
var subtotal = 0;
var table = $('table tbody');
table.find('tr').each(function (i, el) {
// find each <td>
var $tds = $(this).find('td');
// total is in a span in the 4th <td>
total = parseFloat($tds.eq(4).find('span').text().replace(',', '.'));
// add the totals to get the subtotal
subtotal += total;
});
// print the subtotal
$('#subtotal').text(subtotal.toFixed(2));
// multiply subtotal * our tax rate
var tax = (subtotal*taxrate);
// print the tax
$('#tax').text(tax.toFixed(2));
// add subtotal and tax to get grand total
var total = (subtotal + tax);
if(total == 0) {
// cart is empty: hide the table and show 'empty' message
$('table#cart').hide();
$('#cart-content').html('<h4 class="text-danger">There are no items in your cart</h4>');
} else {
// print the total
$('#total').text(total.toFixed(2));
}
}
</script>
产品1
$50
$50
产品2
$5,775.00
$5,775.00
函数updateTotals(id、数量){
//更新我们的价格和总成本
var税率=0.08;
var price=parseFloat($('#price-'+id).text().replace(',',');
var ptotal=(价格*数量);
$('#total-'+id).text(ptotal.toFixed(2));
//循环浏览表格并获得更新的总数
var小计=0;
var table=$(“table tbody”);
表.查找('tr')。每个(函数(i,el){
//找到每个
var$tds=$(this.find('td');
//总数在第四节的跨度内
total=parseFloat($tds.eq(4).find('span').text().replace(',',');
//将总数相加以获得小计
小计+=总计;
});
//打印小计
$(“#小计”).text(小计.固定(2));
//乘以小计*我们的税率
风险值税=(小计*税率);
//打印税单
$(#tax').text(tax.toFixed(2));
//将小计和税金相加,得到总计
风险价值总额=(小计+税费);
如果(总计==0){
//购物车为空:隐藏表格并显示“空”消息
$('table#cart').hide();
$(“#购物车内容”).html(“您的购物车中没有任何项目”);
}否则{
//打印总数
$('#total').text(total.toFixed(2));
}
}
代码中的以下行
parseFloat($tds.eq(4).find('span').text().replace(',', '.'))
将
“5775.00”
转换为“5.775.00”
,解析为5.775。您需要删除逗号,而不是用句号替换。这是我的原始代码replace(/,/g',)
。没什么区别,我还是得到了11.55分,不知道还有什么,但这肯定是问题的一部分。有关代码的简化版本,请参阅。将“,”替换为“”,总共产生11550.00美元,而将“,”替换为“”,总共产生11.55美元。请注意,在输出总计时,11555.00中没有逗号,这是因为javascript在转换为字符串时如何格式化数字。但这与删除逗号无关,这样才能正确解析。你说得对。我在两个地方用句号替换逗号,因为我已经筋疲力尽了,所以我没有意识到这一点。你的小提琴向我指出了这一点,它现在起作用了。谢谢:)