开发者问题收集

无法读取未定义的属性“toFixed”(已更正)

2015-08-10
22928

我一直在查看代码并将其分解,但似乎找不到导致错误的原因。我不确定我对 .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;">&#x2714;</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>
2个回答

如果数量不是 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;
}
James Brierley
2015-08-10

问题在于您在 if 语句块中声明了 subtotal 变量。当您在 replaceWith 语句中使用它时,它超出了范围 - 因此出现 undefined 错误。在所有必需块可访问的范围内声明变量。试试这个:

$('#bottle-image').replaceWith('<div id="bottle-image"> <img src="https://nmhfiles.com/images/nsn/650SSO2_FPOF/Soothanol-' + quantity  + 'Bottles.jpg" alt="Soothanol" width="150" /></div>');

var subtotal = 0;
if (quantity == 6) {
    subtotal = 149.85;
}
else if (quantity == 3) {
    subtotal = 99.90;
}
else if (quantity == 1) {
    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>');        
Rory McCrossan
2015-08-10