2010-11-30 64 views
0

計算表單有多個選項,並根據客戶端輸入的內容確定在側欄中加載的產品。產品給出的數量直接反映了該選項的價值。使用數組根據標題或alt屬性來合計值

因此,現在每個產品都有一個總價格,總持續時間和總潛水次數,如下所示。

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; 

所以,現在我們需要一個腳本,讓我們大總價所有的 - 「product_price_total_」 +產品ID).value的 - 和另一個用於總計持續時間,第三個用於總計潛水等..

不知道如何,但一些想法將只是一個數組,只添加具有特定ALT標記或標題標籤的字段。

任何人都有任何想法。

謝謝

+0

請提供您的產品ID的數組,你可以遍歷? – nemophrost 2010-11-30 23:07:00

+0

問題是,產品ID由CMS ID驅動,我不想列出它們,因爲每次添加新產品時,都必須更改代碼。除非有辦法列出所有可能存在的id。假設我們有所有的產品ID,代碼將如何查看?謝謝。 ps。如果您在上面的代碼中提及了來自運行此函數的單個產品代碼的+產品ID,如下所示: 2010-11-30 23:10:34

+2

你的代碼顯然有冗餘問題。你知道的,對吧? – 2010-11-30 23:17:25

回答

0

這樣的事情呢? (使用相同的$()函數如上SIME維達斯)

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

奇妙的事套褲,感謝您的幫助。非常好的團隊合作是完整的最終代碼。

功能product_analysis(地址,框){ 如果(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; //存儲所有的ID作爲合計數 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); 

}
}

功能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); 

}

}