Javascript 我有两个价格值,我想计算差额
我有两个价格范围。正常价格和报价显示在屏幕上,并呈现以下HTML 我试图使用Javascript/jquery计算差价,并插入一个带有折扣价格的divJavascript 我有两个价格值,我想计算差额,javascript,jquery,Javascript,Jquery,我有两个价格范围。正常价格和报价显示在屏幕上,并呈现以下HTML 我试图使用Javascript/jquery计算差价,并插入一个带有折扣价格的div <div class="price-box"> <p class="old-price"> <span class="price-label">Regular Price:</span> <span class="price" id="old-price
<div class="price-box">
<p class="old-price">
<span class="price-label">Regular Price:</span>
<span class="price" id="old-price-221">Rs17.00 </span>
</p>
<p class="special-price">
<span class="price-label">Special Price</span>
<span class="price" id="product-price-221">Rs14.45 </span>
</p>
<div>
<span>Discount Price:</span>
<span>XXXXXX</span>
</div>
</div>
正常价格:
Rs17.00
特价
Rs14.45
折扣价格:
XXXXXX
我有这个货币符号也显示在价格上。所以,我想知道如何计算“正常价格”和“特殊价格”之间的差异
谁能帮帮我吗
顺便说一句,我搜索了这个网站,没有找到相关的答案。在那里:
jQuery部分:
$('.price-box').each(function(){
var element = $(this),
oldPriceRaw = element.find('.old-price .price').text(),
specialPriceRaw = element.find('.special-price .price').text(),
oldPrice = parseFloat(oldPriceRaw.replace('Rs','')),
specialPrice = parseFloat(specialPriceRaw.replace('Rs','')),
diff = 'Rs'+(oldPrice - specialPrice).toFixed(2), // 2 numbers after the comma
diffElement = element.find('.diff-price');
diffElement.text(diff);
});
HTML有点修改:
<div class="price-box">
<p class="old-price">
<span class="price-label">Regular Price:</span>
<span class="price" id="old-price-221">Rs17.00 </span>
</p>
<p class="special-price">
<span class="price-label">Special Price</span>
<span class="price" id="product-price-221">Rs14.45 </span>
</p>
<div>
<span>Discount Price:</span>
<span class="diff-price"></span>
</div>
</div>
正常价格:
Rs17.00
特价
Rs14.45
折扣价格:
棘手的事情是将当前价格值作为浮点数。然后,我们只是计算差额。货币是否总是Rs?计算出您的逻辑(价格x-价格y),计算出如何从元素中获得值,然后进行数学逻辑。这很简单,先尝试一下,然后寻求帮助。我们不会为您编写代码;)也许你要找的是浮动汇率是的,货币永远是卢比。