javascript - Calculating Values loaded in via jquery -
a continuation solved case new problem. following code calculates values loaded in via ajax script. series of options in form determine information loaded in , therefore final calculations are.
the problem once have loaded in available options cannot load them out , script adds field totals in fact fail @ point. example if load in 2 options had totals. load in third , add total of 3. take 1 option out , script fails. believe way script fetches product ids — var productids = {};
any appreciated
here code
var productids = {}; function product_totals(id) { productids[id] = true; // store id's totals calculated var quantity = $c('product_quantity_' + id).value; var price = $c('product_price_' + id).value; var duration = $c('product_duration_' + id).value; var dives = $c('product_dives_' + id).value; var hire = $c('product_hire_' + id).value; number($c('product_price_total_' + id).value = price * quantity); number($c('product_duration_total_' + id).value = duration * quantity); number($c('product_dives_total_' + id).value = dives * quantity); number($c('product_hire_total_' + id).value = hire * quantity); function $c(id) { return document.getelementbyid(id); } var totalpricetotal = 0; var totaldurationtotal = 0; var totaldivestotal = 0; var totalhiretotal = 0; (var id in productids) { // multiply 1 make sure it's number totalpricetotal += $c('product_price_total_' + id).value * 1; totaldurationtotal += $c('product_duration_total_' + id).value * 1; totaldivestotal += $c('product_dives_total_' + id).value * 1; totalhiretotal += $c('product_hire_total_' + id).value * 1; } $c('gt_total_price').value = totalpricetotal; $c('gt_total_duration').value = totaldurationtotal; $c('gt_total_dives').value = totaldivestotal; $c('gt_total_hire').value = totalhiretotal; function $c(id) { return document.getelementbyid(id); } } working sample @ http://www.divethegap.com/update/diving-trips/adventure-training/#level_01 (click on beginners) many thanks
if understand, trying achieve stack collection of ids in array can remove items can calculate selected options. declare var productids = []; , add ids in using productids.push(id). when remove option use productids.splice(indexofid, 1);. when recalcultate have ids want.
Comments
Post a Comment