使用jquery将值一起添加 [英] Add values together using jquery

查看:88
本文介绍了使用jquery将值一起添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个标签可以保存每件商品和运费的价格。我需要将三个span标签一起添加到使用jQuery的总价格中。这是我的代码:

 < div id =relative> 
< div id =absolute>
Widget 1:< span id =widget_1_price> $ 99.99< / span>< br />
Widget 2:< span id =widget_2_price> $ 14.99< / span>< br />
运费:< span id =shipping_price> $ 10.00< / span>< br />
< b>总计:< span id =total_price>< / span>< / b>
< / div>
< / div>

我尝试了几种方法,但没有一种适合我。



 

> var sum = 0;
$('#widget_1_price,#widget_2_price,#shipping_price')。each(function(){
sum + = parseFloat($(this).text()。substr(1));
});
$('#total_price')。text('$'+ Math.round(sum * 100)/ 100);

演示: http://jsfiddle.net/QTMsE/


I have 3 span tags that hold the price for each item and shipping. I need to add the three span tags together to come up with the total price using jQuery. Here is my code:

<div id="relative">
    <div id="absolute">
        Widget 1: <span id="widget_1_price">$99.99</span><br />
        Widget 2: <span id="widget_2_price">$14.99</span><br />
        Shipping Fee: <span id="shipping_price">$10.00</span><br />
        <b>Total: <span id="total_price"></span></b>
    </div>
</div>

I have tried several methods but none seem to work for me.

解决方案

Loop through the elements and parse the text in them, and add them together:

var sum = 0;
$('#widget_1_price,#widget_2_price,#shipping_price').each(function(){
  sum += parseFloat($(this).text().substr(1));
});
$('#total_price').text('$' + Math.round(sum * 100) / 100);

Demo: http://jsfiddle.net/QTMsE/

这篇关于使用jquery将值一起添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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