我一直在看代码并将其分解,但是我似乎找不到导致错误的原因。我不确定.toFixed我在做什么错,这破坏了代码。你能帮我吗?
代码(已更正)
<script type="text/javascript">
$(document).ready(function(){
function getTotal() {
var quantity = $('#tmp_quantity').val();
var name = "formComponentsMap['order'].countryName";
var countryValue = $('input[name="' + name + '"]').val();
var tax = 0.00;
var shipping = 0.00;
var stateName = "formComponentsMap['order'].stateId";
var stateValue = $('select[name="' + stateName + '"]').val();
// If state value is Maryland (33) need to add 6% sales tax
if (stateValue == 33) {
tax = .06;
}
if ($('#ADS_INTL').is(':checked') || $('#ADS_US').is(':checked')) {
if ($('#ADS_INTL').is(':checked') && quantity == 1) {
shipping = 14.95;
var ads = '';
}
else {
shipping = 0.00;
var ads = '<span style="color: #A3BF3F;">✔</span> with Auto-Delivery Service';
}
}
else {
var ads = '';
if (countryValue != "UNITED STATES" || typeof countryValue == 'undefined') {
shipping = 14.95;
}
else {
shipping = 6.95;
}
}
var subtotal = 0.00;
$('#quantity').replaceWith('<div id="quantity">' + quantity + '</div>');
if (quantity == 6) {
$('#bottle-image').replaceWith('<div id="bottle-image"> <img src="https://nmhfiles.com/images/nsn/650SSO2_FPOF/Soothanol-6Bottles.jpg" alt="Soothanol" width="150" /></div>');
subtotal = 149.85;
}
else if (quantity == 3) {
$('#bottle-image').replaceWith('<div id="bottle-image"> <img src="https://nmhfiles.com/images/nsn/650SSO2_FPOF/Soothanol-3Bottles.jpg" alt="Soothanol" width="150" /></div>');
subtotal = 99.90;
}
else if (quantity == 1) {
$('#bottle-image').replaceWith('<div id="bottle-image"> <img src="https://nmhfiles.com/images/nsn/650SSO2_FPOF/Soothanol-1Bottle.jpg" alt="Soothanol" width="150" /></div>');
subtotal = 49.95;
}
$('#ads').replaceWith('<div id="ads">' + ads + '</div>');
$('#subtotal').replaceWith('<div id="subtotal">' + subtotal.toFixed(2) + '</div>');
$('#tax').replaceWith('<div id="tax">' + (tax * subtotal).toFixed(2) + '</div>');
$('#shipping').replaceWith('<div id="shipping">' + shipping.toFixed(2) + '</div>');
var total = subtotal + (tax * subtotal) + shipping;
$('#total').replaceWith('<div id="total">' + total.toFixed(2) + '</div>');
}
$("div[class^='tim']").click(function(){
var radioValue = $(this).attr('id');
if (typeof radioValue != "undefined") {
var quantity = radioValue.split('_timSelect_');
$('#tmp_quantity').val(quantity[1]);
var quantity = $('#tmp_quantity').val();
getTotal();
}
});
$('#__billToZipCode').click(function(){
getTotal();
});
$('#ADS_US').click(function(){
getTotal();
});
$('#ADS_INTL').click(function(){
getTotal();
});
});
</script>
最佳答案
如果数量不为1、3或6,则会出错,因为不会设置小计。尝试将其更改为:
var subtotal = 0;
if (quantity == 6) {
$('#bottle-image').replaceWith('<div id="bottle-image"> <img src="https://nmhfiles.com/images/nsn/650SSO2_FPOF/Soothanol-6Bottles.jpg" alt="Soothanol" width="150" /></div>');
subtotal = 149.85;
} else if (quantity == 3) {
$('#bottle-image').replaceWith('<div id="bottle-image"> <img src="https://nmhfiles.com/images/nsn/650SSO2_FPOF/Soothanol-3Bottles.jpg" alt="Soothanol" width="150" /></div>');
subtotal = 99.90;
} else if (quantity == 1) {
$('#bottle-image').replaceWith('<div id="bottle-image"> <img src="https://nmhfiles.com/images/nsn/650SSO2_FPOF/Soothanol-1Bottle.jpg" alt="Soothanol" width="150" /></div>');
subtotal = 49.95;
}
关于javascript - 无法读取未定义的属性 'toFixed'(已更正),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31927916/