2016-03-19 86 views
0

任何想法爲什麼我試圖一起計算的總數是1.00?jQuery計算兩個值錯誤,不知道爲什麼

$(document).ready(function() { 
 
    // Hide initial values that need updating 
 
    $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").hide(); 
 
    // get current delivery rate 
 
    $("#get_rate").trigger('click'); 
 
    // set a timeout and get total and shipping that was generated and add together for nnew total 
 
    setTimeout(function() { 
 
    // get cart sub-total 
 
    var a = $(".cart-total span").html().replace(/[$£]/gi, ""); 
 
    var ib = $("#estimated-shipping em").html().replace(/[$£]/gi, ""); 
 

 
    if (ib == "FREE") { 
 
     $("#estimated-shipping em").html("FREE"); 
 
     var b = "0.00"; 
 
    } else { 
 
     var b = parseFloat($("#estimated-shipping em").text().replace(/\$|£/gi, "")); 
 
    } 
 

 
    // add together sub-total and estimated shipping to new total 
 
    // update with new total with sub-total and added shipping 
 
    var total = parseFloat(a) + parseFloat(b); 
 
    total = parseFloat(Math.round(total * 100)/100).toFixed(2); 
 
    $('.cart-finalTotal span').text("£" + total); 
 

 
    // show new values 
 
    $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").show(); 
 
    }, 2000); 
 
    $(".item-quantity input").on("change", function() { 
 
    document.location.href = location.href 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="cart-right"> 
 

 

 
    <p class="cart-total">Sub-Total<span class="money">£1,245.00</span> 
 
    </p> 
 
    <p class="cart-vat">VAT 20% (included)<span class="money" style="display: block;">£249.00</span> 
 
    </p> 
 
    <p class="cart-delivery">Delivery (estimated)<span class="money" id="estimated-shipping">+ <em style="display: inline;">FREE</em></span> 
 
    </p> 
 
    <p class="cart-finalTotal">Total<span class="money" style="display: block;">£0.00</span> 
 
    </p> 
 

 
</div>

+0

要添加0和1? –

+0

我是?我做了一個控制檯登錄和得到1,245.00和0.00作爲免費送貨 – James

+0

當我測試代碼,它似乎確定,直到購物車總數超過1000,然後設置爲1? – James

回答

1

在你的小計,你有一個逗號,parseFloat會當它看到一個非數字字符不是一個點停止。所以parse float返回1而不是1245.你需要你的正則表達式更多的是/[^0-9.]/g的行。

這裏的代碼工作:

$(document).ready(function() { 
    // Hide initial values that need updating 
    $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").hide(); 
    // get current delivery rate 
    $("#get_rate").trigger('click'); 
    // set a timeout and get total and shipping that was generated and add together for nnew total 
    setTimeout(function() { 
    // get cart sub-total 
    var a = $(".cart-total span").html().replace(/[^0-9.]/gi, ""); 

    if ($("#estimated-shipping em").text() == "FREE") { 
     $("#estimated-shipping em").html("FREE"); 
     var b = "0.00"; 
    } else { 
     var b = parseFloat($("#estimated-shipping em").text().replace(/[^0-9.]/gi, "")); 
    } 

    // add together sub-total and estimated shipping to new total 
    // update with new total with sub-total and added shipping 
    var total = parseFloat(a) + parseFloat(b); 
    total = parseFloat(Math.round(total * 100)/100).toFixed(2); 
    $('.cart-finalTotal span').text("£" + total); 

    // show new values 
    $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").show(); 
    }, 2000); 
    $(".item-quantity input").on("change", function() { 
    document.location.href = location.href 
    }); 
}); 
+0

對不起,我會改變我當前的代碼/正則表達式...對不起,JS是如此新,我仍然在學習..... – James

+0

更改所有.replace(/ [$£]/gi ,「」)替換爲.replace(/ [^ 0-9。/ gi,「」) – Beamer180

+0

是否會找到並替換$和£? – James

相關問題