任何人都有任何想法为什么最终总数不能正确计算?它似乎在增加价值,所以像249.00 + 9.00变成2499而不是258?
$(document).ready(function(){
// set free shipping as false as default
var isFree = "false";
// 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 = parseFloat($(".cart-total span").text().replace(/\$|£/gi, ""));
// get estimated shipping cost
var b = parseFloat($("#estimated-shipping em").text().replace(/\$|£/gi, ""));
if(b == "FREE"){
b = "0.00"
var isFree = "true";
} else {
b = $("#estimated-shipping em").text().replace(/\$|£/,'');
}
// add together sub-total and estimated shipping to new total
var total = parseFloat(a + b).toFixed(2);
//add the currency with £ (not sure why it was setting $ before)
if(isFree == "true") {
$("#estimated-shipping em").html("FREE");
} else {
$("#estimated-shipping em").html("£"+ b);
}
// update with new total with sub-total and added shipping
$('.cart-finalTotal span').html("£" + total);
// show new values
$("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").show();
}, 2000);
$(".item-quantity input").on("change", function() {
// let initially disable the input to avoid problems
$(".item-quantity input").prop( "disabled", true );
// gets auto get rates based on a UK address
$("#get_rate").trigger('click');
$("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").hide();
setTimeout(function(){
var a = parseFloat($(".cart-total span").text().replace(/\$|£/gi, ""));
// get estimated shipping cost
var b = parseFloat($("#estimated-shipping em").text().replace(/\$|£/gi, ""));
if(b == "FREE"){
b = "0.00"
var isFree = "true";
} else {
b = $("#estimated-shipping em").text().replace(/\$|£/gi, "");
}
// add together sub-total and estimated shipping to new total
var total = parseFloat(a + b).toFixed(2);
//add the currency with £ (not sure why it was setting $ before)
if(isFree == "true") {
$("#estimated-shipping em").html("FREE");
} else {
$("#estimated-shipping em").html("£"+ b);
}
// update with new total with sub-total and added shipping
$('.cart-finalTotal span').html("£" + total);
// update VAT when input changed
var newVAT = (parseFloat($(".cart-total span").text().replace(/\$|£/gi, "")) * 20) /100;
$(".cart-vat span").html("£"+ newVAT.toFixed(2));
// show all new value updates
$("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").show();
// lets enable the quantity input a gain
$(".item-quantity input").prop( "disabled", false);
}, 2000);
});
});
<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">£249.00</span>
</p>
<p class="cart-vat">VAT 20% (included)<span class="money">£49.80</span>
</p>
<p class="cart-delivery">Delivery<span class="money" id="estimated-shipping">+ <em>$9.00</em></span>
</p>
<p class="cart-finalTotal">Total<span class="money">£249.00</span>
</p>
<div class="cart-checkout">
<button class="button button-primary button-add-to-cart button-pay-now" type="submit" name="checkout"><span class="icom-lock"></span>Pay Now</button>
<br>
<br>
<div class="additional-checkout-buttons">
<p>Or checkout with</p>
<img id="applePayButton" style="display: none" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" onload="typeof createApplyPayButton === 'function' ? createApplyPayButton(this) : window.addEventListener('applePayReady', (function(){createApplyPayButton(this)}).bind(this))">
<input type="image" name="goto_pp" value="paypal_express" src="https://www.paypalobjects.com/en_US/i/btn/btn_xpressCheckout.gif">
</div>
</div>
</div>
答案 0 :(得分:0)
找到包含var total = parseFloat(a + b).toFixed(2);
的行,并在进程发生之前根据需要parseFloat来替换为var total = (parseFloat(a) + parseFloat(b)).toFixed(2);
。
$(document).ready(function(){
// set free shipping as false as default
var isFree = "false";
// 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 = parseFloat($(".cart-total span").text().replace(/\$|£/gi, ""));
// get estimated shipping cost
var b = parseFloat($("#estimated-shipping em").text().replace(/\$|£/gi, ""));
if(b == "FREE"){
b = "0.00"
var isFree = "true";
} else {
b = $("#estimated-shipping em").text().replace(/\$|£/,'');
}
// add together sub-total and estimated shipping to new total
var total = (parseFloat(a) + parseFloat(b)).toFixed(2);
//add the currency with £ (not sure why it was setting $ before)
if(isFree == "true") {
$("#estimated-shipping em").html("FREE");
} else {
$("#estimated-shipping em").html("£"+ b);
}
// update with new total with sub-total and added shipping
$('.cart-finalTotal span').html("£" + total);
// show new values
$("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").show();
}, 2000);
$(".item-quantity input").on("change", function() {
// let initially disable the input to avoid problems
$(".item-quantity input").prop( "disabled", true );
// gets auto get rates based on a UK address
$("#get_rate").trigger('click');
$("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").hide();
setTimeout(function(){
var a = parseFloat($(".cart-total span").text().replace(/\$|£/gi, ""));
// get estimated shipping cost
var b = parseFloat($("#estimated-shipping em").text().replace(/\$|£/gi, ""));
if(b == "FREE"){
b = "0.00"
var isFree = "true";
} else {
b = $("#estimated-shipping em").text().replace(/\$|£/gi, "");
}
console.log(a)
// add together sub-total and estimated shipping to new total
var total = (parseFloat(a) + parseFloat(b)).toFixed(2);
//add the currency with £ (not sure why it was setting $ before)
if(isFree == "true") {
$("#estimated-shipping em").html("FREE");
} else {
$("#estimated-shipping em").html("£"+ b);
}
// update with new total with sub-total and added shipping
$('.cart-finalTotal span').html("£" + total);
// update VAT when input changed
var newVAT = (parseFloat($(".cart-total span").text().replace(/\$|£/gi, "")) * 20) /100;
$(".cart-vat span").html("£"+ newVAT.toFixed(2));
// show all new value updates
$("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").show();
// lets enable the quantity input a gain
$(".item-quantity input").prop( "disabled", false);
}, 2000);
});
});
<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">£249.00</span>
</p>
<p class="cart-vat">VAT 20% (included)<span class="money">£49.80</span>
</p>
<p class="cart-delivery">Delivery<span class="money" id="estimated-shipping">+ <em>$9.00</em></span>
</p>
<p class="cart-finalTotal">Total<span class="money">£249.00</span>
</p>
<div class="cart-checkout">
<button class="button button-primary button-add-to-cart button-pay-now" type="submit" name="checkout"><span class="icom-lock"></span>Pay Now</button>
<br>
<br>
<div class="additional-checkout-buttons">
<p>Or checkout with</p>
<img id="applePayButton" style="display: none" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" onload="typeof createApplyPayButton === 'function' ? createApplyPayButton(this) : window.addEventListener('applePayReady', (function(){createApplyPayButton(this)}).bind(this))">
<input type="image" name="goto_pp" value="paypal_express" src="https://www.paypalobjects.com/en_US/i/btn/btn_xpressCheckout.gif">
</div>
</div>
</div>
答案 1 :(得分:0)
您错过了parseFloat
,这会导致一些问题。变化:
else {
b = $("#estimated-shipping em").text().replace(/\$|£/,'');
}
到
else {
b = parseFloat($("#estimated-shipping em").text().replace(/\$|£/,''));
}
实际问题发生在另一个答案中提到的那一行。