我想要做的就是检查每个输入的价格,并将其作为总和。jQuery提取数据并制作金额
这里是我的代码
function totalSum(e) {
e.preventDefault();
var unit = $("input:checked").parent("dt").siblings("dd").find("span");
total = 0;
$.each(unit, function(index, obj){
total += parseInt($(obj).text(), 10);
});
$("#totalPrice").html('<span class="count">€ ' + total + '</span> €');
}
每个单元的跨度内找到。 Total设置为0.我尝试在每个检查对象上调用parseInt,然后在一个范围内添加总和。在HTML中,价格是这样表述的:
<dd><span class="costo">€199</span></dd>
所以当你看到有欧元标记。恐怕不能解析,这是吗?因为没有改变!我应该如何写它? 在此先感谢
好吧,我感到很惭愧,但我不能得到它的工作。我决定把代码在其最小,所以我想这种方式
<body>
<div class="bla"><span class="count">1</span></div>
<div class="bla"><span class="count">1</span></div>
<div class="bla"><span class="count">1</span></div>
<div id="total"></div>
<script type="text/javascript" src="js/jquery-1.9.0.min.js" /></script>
<script>
$(document).ready(function(){
function sum() {
var prices = $("div.bla").find(".count");
total= 0;
$.each(prices, function(index, obj){
total += parseFloat($(obj).text());
});
$("#total").html('<span class="count">'+total +'</span> €');
};
});
这应该工作,但没有出现。有人能告诉我发生了什么事吗?!
检查标志,并从中删除一个优雅的解决方案解析之前的字符串。 –
http://stackoverflow.com/questions/559112/how-to-convert-a-currency-string-to-a-double-with-jquery-or-javascript – Dharman