所以我准备好了我的jQuery脚本,但我正在努力解决1个问题,如果一个值是:$ 10那么一切都很好,但是当一个值是:$24,99
那么它只会显示{{1而不是24
。
我该如何解决这个问题?这是我的来源:
http://jsfiddle.net/0L2n8wdt/34/
24,99
<table width="652" border="0" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th align="left">
<strong>Product</strong>
</th>
<th align="left">
<strong>Product Price</strong>
</th>
<th align="left">
<strong>How much you want?</strong>
</th>
<th align="left">
<strong>Total Price</strong>
</th>
</tr>
</thead>
<tbody>
<tr>
<td width="210">Fruit:</td>
<td width="216">$ 10</td>
<td width="204">
<input type="text" />
</td>
<td width="204"></td>
</tr>
<tr>
<td>Drinks:</td>
<td>$ 22,95</td>
<td>
<input type="text" />
</td>
<td></td>
</tr>
<tr>
<td>Cards:</td>
<td>$ 5</td>
<td>
<input type="text"/>
</td>
<td></td>
</tr>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td>Total price of all:</td>
<td id="total"></td>
</tr>
</tfoot>
</table>
答案 0 :(得分:1)
这种情况正在发生,因为您使用了parseInt
,请使用parseFloat
。
其次,值22,95
应为22.95
。
var unitAmount = parseFloat($input.parent().prev().text().replace("$", ""));
total += parseFloat($(this).text().replace("€","") || 0);
答案 1 :(得分:1)
您必须将小数点分隔符更改为点,将parseInt
更改为parseFloat
var unitAmount = parseFloat($input.parent().prev().text().replace("$", "").replace(",","."))
在算术运算后,您必须将数字转换回字符串并用,
替换点:
$input.parent().next().text("€ " + (""+total).replace(".",","));
至于价格:
var total = 0;
$("tbody tr td:last-child").each(function() {
total += parseFloat($(this).text().replace("€","").replace(",",".") || 0);
});
$("#total").html( (""+total).replace(".",","));
完整演示为here
答案 2 :(得分:0)
$input.parent().next().text(total > 0 ? ("€ "+total).replace(".",",") : "");
这应该有用,你会看到,而不是。
答案 3 :(得分:-1)
因为您使用的是parseInt,所以请改用parseFloat