首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Jquery计算有效,但在正确的值之前添加了不正确的值

Jquery计算有效,但在正确的值之前添加了不正确的值
EN

Stack Overflow用户
提问于 2019-01-22 20:25:10
回答 1查看 58关注 0票数 0

我一直在研究这个计算器,从某种意义上说,它可以正确地计算出土地的价格并显示出来。但问题是,它似乎将正确的答案附加到另一个值(假设取自数据价格,因为它们相互关联)。

下面是我的代码:

代码语言:javascript
运行
复制
var updateTotal = function() {
 
    var sumtotal;
       var sum = 0;
    //Add each product price to total
    $(".extraproduct").each(function() {
        var extra_price = $(this).data('price');
        var quantity = $('.extraQuantity', this).val();
   
        //Total for one product
        var subtotal = extra_price*quantity;     
         //Round to 2 decimal places.
        subtotal = subtotal.toFixed(2);      
        //Display subtotal in HTML element
        $('.productTotal', this).html(subtotal);
    });
    // total
       $('.productTotal').each(function() {
        sum += Number($(this).html());
    }); 
    
    $('#sum').html(sum.toFixed(2));
};
//Update total when quantity changes
$(".extraQuantity").bind("keyup change", function() {
    updateTotal();
});
//Update totals when page first loads
updateTotal();
// set this from local
    $('span.productTotal').each(function() {
        $(this).before("£")
    }); 
// unit price
   $('.extraproduct span').each(function() {
       var $price = $(this).parents("div").data('price');
      $(this).before($price);
    }); 
代码语言:javascript
运行
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<span class="quote-label">Product 1</span>
<div class="input-wrap currency extraproduct" data-price="450.00">
<input type="number" class="extraQuantity" value="0"  min="1" max="20" />
<span class="productTotal"></span>
</div>
                
 <span class="quote-label">Product 2</span>
<div class="input-wrap currency extraproduct" data-price="10.00">
<input type="number" class="extraQuantity" value="0"  min="1" max="20" />
<span class="productTotal"></span>
</div>


            
 <span class="quote-label">Additional Product</span>
<div class="input-wrap checkbox-wrap extraproduct" data-price="5.00"><input type="checkbox" class="extraQuantity" value="0" />
<span class="productTotal"></span>
</div>

<div id="total">Total: £<span id="sum">0.00</span> </div>

https://jsfiddle.net/DigitalAdam/5fLb0vnp/26/

我认为这可能与subtotal.toFixed部分有关,但在调整或删除它时没有运气。任何帮助都是非常感谢的。问候

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-22 20:45:03

非常简单的解决方案:您只是使用.productTotal跨度来计算一些东西。您可以将显示设置为none。它仍然可以用于计算,但现在将显示:

代码语言:javascript
运行
复制
.productTotal {
  display: none;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54308283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档