javascript - JS script to calculates sum of amount calculates wrong -
i have prepared this jsfiddle ilustrates how script calculate twice sum of each selected option attribute price. please me solve issue.
optionsamount
wrong, mean calculated twice.. why that? thanks
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); }
try this,
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);
Comments
Post a Comment