跨度jQuery的总和 [英] sum total from spans jQuery

查看:91
本文介绍了跨度jQuery的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个jQuery函数以计算跨度的总数

I am trying to build a jQuery function to calculate the total number from the span

var sumnormaltotal = 0;
$('span[id^="normaloffertotalspan"]').each(function () {
    var text = $(this).text();
    sumnormaltotal += parseInt(text, 10);
});

该功能运行正常,但是我有一些产品的价格以英镑符号开头,例如£1.50,但我的返回结果为1,而不是1.50,因此例如,如果我为特定产品选择金额2该价格返回的结果将是2而不是3,您能帮我提供一些想法吗?

The function is working perfectly but I have some products where the price is starting with a pound sign like £1.50, but my result back will be 1, not 1.50 so for example if I choose the amount 2 for the specific product with that price the result back will be 2 instead of 3 can you help me with some ideas please.

推荐答案

尝试一下..

var sum = 0;
$('.normaloffertotalspan').each(function() {
  sum += +$(this).text()||0;
});
$("#subtotal").text(sum);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div >

 <span class="normaloffertotalspan" id="option-1">11.11</span><br>
 <span class="normaloffertotalspan" id="option-2">22.5</span><br>
 <span class="normaloffertotalspan" id="option-3">33.33</span>

</div>

<h3>Subtotal = <span id="subtotal">0.00</span></h3>

这篇关于跨度jQuery的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆