简体   繁体   中英

Use arrays to add up values based on the title or the alt attribute

Calculating form has options and based on what the client enters determines what products load in a sidebar. Products are given a quantity that directly reflects that value of the option.

So now per product there is a Total Price, Total Duration and Total Number of Dives as seen here.

num1=Number(document.getElementById('product_quantity_' + productid).value);
num2=Number(document.getElementById('product_price_' + productid).value);
nums=num1*num2;
document.getElementById('product_price_total_' + productid).value = nums;

num1=Number(document.getElementById('product_quantity_' + productid).value);
num2=Number(document.getElementById('product_duration_' + productid).value);
nums=num1*num2;
document.getElementById('product_duration_total_' + productid).value = nums;

num1=Number(document.getElementById('product_quantity_' + productid).value);
num2=Number(document.getElementById('product_dives_' + productid).value);
nums=num1*num2;
document.getElementById('product_dives_total_' + productid).value = nums;

num1=Number(document.getElementById('product_quantity_' + productid).value);
num2=Number(document.getElementById('product_hire_' + productid).value);
nums=num1*num2;
document.getElementById('product_hire_total_' + productid).value = nums;

So now we need a script that gives us the Grand Total Price of all of the -- 'product_price_total_' + productid).value -- and another for Grand Total Duration, and a third for Grand Total Dives etc.. etc...

Not sure how but a few ideas would be an array that added up only fields with a specific alt tag or title tag.

Anyone got any ideas.

Thanks

How about something like this? (Using the same $() function as Šime Vidas above)

var productIds = {};

function product_totals(id) {
    productIds[id] = true; // store all id's as the totals are calculated
    ....
}

function totalTotals() {
    var totalPriceTotal = 0;
    var totalDurationTotal = 0;
    var totalDivesTotal = 0;
    var totalHireTotal = 0;

    for (var id in productIds) {
        // multiply by 1 to make sure it's a number
        totalPriceTotal += $('product_price_total_' + id).value*1;
        totalDurationTotal += $('product_duration_total_' + id).value*1;
        totalDivesTotal += $('product_dives_total_' + id).value*1;
        totalHireTotal += $('product_hire_total_' + id).value*1;
    }
}

Marvellous work chaps, thanks for you help. Very good team work here is the full final code.

function product_analysis(address, box) { if (box.checked) {

$('#product_' + box.alt).load(address);

}
else {

$('#product_' + box.alt).load('http://www.divethegap.com/update/blank.html');

}
document.getElementById('product_quantity_PRI_' + box.alt).value = box.value;

};

var productIds = {};

function product_totals(id) { productIds[id] = true; // store all id's as the totals are 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);

}
}

function totalTotals() { var totalPriceTotal = 0; var totalDurationTotal = 0; var totalDivesTotal = 0; var totalHireTotal = 0;

for (var id in productIds) {
    // multiply by 1 to make sure it's a 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);

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM