2014-12-16 26 views
5

Tôi đã chuẩn bị this jsfiddle mà ilustrates cách tập lệnh của tôi tính hai lần tổng của mỗi giá thuộc tính tùy chọn đã chọn. Làm ơn giúp tôi giải quyết vấn đề này.JS script để tính tổng số tiền tính sai

optionsamount sai, ý tôi là được tính hai lần .. Tại sao vậy? Cảm ơn

function update_amounts(){ 
    var sum = 0.0; 
    var optionsamount = 0.0; 

    $('#basketorder > tbody > .product').each(function() { 

     $('.selectedoptionselect option:selected').each(function(){ 
      optprice = $(this).attr('price'); 
      optionsamount+= parseFloat(optprice);  
     }) 

     var qty = $(this).find('.qty option:selected').val(); 
     var price = $(this).find('.price').val(); 
     var amount = (qty*price); 
     sum+= (amount + optionsamount); 

     $(this).find('.amount').text(''+ amount.toFixed(2)); 
    }); 

    $('.total').text(sum); 
} 
+0

trong fiddle bạn không bao giờ gọi hàm 'update_amounts() ' – caramba

+0

thats không phải là điểm tôi tin :) – Europeuser

+0

Vâng, fiddle là khá vô ích. – forgivenson

Trả lời

3

Hãy thử điều này,

function update_amounts(){ 
var sum = 0.0; 

$('#basketorder > tbody > .product').each(function() { 

    var optionsamount = 0.0; 


    $(this).find('.selectedoptionselect option:selected').each(function(){ 
     optprice = $(this).attr('price'); 
     optionsamount+= parseFloat(optprice);  
    }) 

    var qty = $(this).find('.qty option:selected').val(); 
    var price = $(this).find('.price').val(); 
    var amount = (qty*price); 
    sum+= (amount + optionsamount); 

    $(this).find('.amount').text(''+ amount.toFixed(2)); 
}); 

$('.total').text(sum); 
+0

Vaivhav, có vẻ như bạn là người đàn ông! :) cảm ơn bạn rất nhiều ! Người bạn đời sai lầm là gì? – Europeuser

+1

Bộ chọn jquery ('.selectedoptionselect option: selected') đang chạy mỗi lần cho tất cả các sản phẩm ... – Vaibhav

+0

Một lần nữa Cảm ơn bạn Vaibhav! – Europeuser

0

thử này

function update_amounts(){ 
var sum = 0.0; 

$('#basketorder > tbody > .product').each(function() { 

    var optionsamount = 0.0; 


    $('.selectedoptionselect option:selected').each(function(){ 
     optprice = $(this).attr('price'); 
     optionsamount+= parseFloat(optprice);  
    }) 

    var qty = $(this).find('.qty option:selected').val(); 
    var price = $(this).find('.price').val(); 
    var amount = (qty*price); 
    sum+= (amount + optionsamount); 

    $(this).find('.amount').text(''+ amount.toFixed(2)); 
}); 

$('.total').text(sum); 

}

+0

Alex nó vẫn tính toán double optionsamount – Europeuser

Các vấn đề liên quan