2015-08-10 4 views
1

Я искал код и разбивал его, но я не могу найти, что вызывает ошибку. Я не уверен, что я делаю неправильно с .toFixed, который нарушает код. Вы можете помочь?Невозможно прочитать свойство «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> 
+0

В общем, это означает, что вещь в левой части '.' в' .toFixed() '' undefined'. В вашем коде 'subtotal' получает значение только в определенных ситуациях, но не в других. – Pointy

ответ

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; 
} 
+0

, который исправил ошибку, спасибо. теперь я не могу получить свои итоги для отображения. Я показываю только стоимость доставки. – user4739731

1

Проблемы в том, что вы декларировании subtotal переменных внутри в if операторных блоков. Затем он выходит за пределы области действия, когда вы используете его в операциях 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>');   
+0

спасибо. который фиксировал ошибку. теперь я не показываю значения. не знаю почему. отображается только стоимость доставки. – user4739731

+0

Не могли бы вы создать рабочий пример в http://jsfiddle.net. Легче следовать логике, когда вы видите, что она работает. –

+0

Я попытаюсь, но мой код обычно не работает в jsfiddle из-за системы, которую я использую. – user4739731

Смежные вопросы