使用jquery将值一起添加

编程入门 行业动态 更新时间:2024-10-27 08:24:55
本文介绍了使用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);

演示: jsfiddle/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: jsfiddle/QTMsE/

更多推荐

使用jquery将值一起添加

本文发布于:2023-11-22 00:22:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:jquery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!