2010-11-30 11 views
0

計算フォームにはオプションがあり、クライアントの入力内容に基づいて、どの製品がサイドバーに読み込まれるかが決まります。製品には、オプションの価値を直接反映する数量が与えられます。配列を使用してタイトルまたはalt属性に基づいて値を加算します

ここでは、製品ごとに合計価格、合計所要時間、およびダイビングの総数が表示されます。総計ダイブのため、グランド合計期間のための別の、第三 - 「product_price_total_」+のProductID).VALUE -

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; 

は、だから今、私たちは私たちのすべての総計価格を与えるスクリプトが必要

特定のaltタグまたはtitleタグを持つフィールドのみを追加した配列は、ほんの少しのアイデアになるでしょうか。

誰でも知っています。

ありがとうございました

+0

あなたはを通してループ製品のIDの配列を可能性がありますか? – nemophrost

+0

問題は、製品IDがCMS IDによって駆動されているため、新しい製品を追加するたびにコードを変更する必要があるたびに、そのIDをリストしたくないということです。存在するかもしれないすべてのIDをリストする方法がない限り。私たちがすべての製品IDを持っていたとすれば、コードはどのように見えるでしょうか?ありがとう。 ps。上記のコードの+プロダクトIDは、このような機能を実行する個々のプロダクトコードに由来しています。

+2

あなたのコードには明らかに冗長性の問題があります。あなたはそれを知っていますよね? –

答えて

0

どのようにしたら?あなたは助けるために(上記のサイムVIDASと同じ$()関数を使用して)

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

こんにちは、ありがとうございました、あなたのコードはすべて上記の答えに実装されています。しかし、まだ1つの問題があります。実装をhttp://www.divethegap.com/update/diving-trips/adventure-training/#で見て、初心者をクリックしてください。チェックボックスをクリックしてすべてのajaxを読み込んだら、ボックスのチェックを外すと、「テスト」によってアクティブ化されたスクリプトは機能しなくなります。何か案は。 –

+0

「test」リンクをクリックすると、totalTotals()が定義されていないというエラーが表示されます。 jQuery関数内で定義した場合は、その関数を使用しているリンクにバインドすることをお勧めします。それ以外の場合は、グローバルスコープでは使用できません。したがって、onclick = "totalTotals(); return false"の代わりに、totalTotalsを定義した後でこれを試してください:$( '#testLink')。 – nemophrost

+0

私はそれがうまくいかなかったコードを修正していました。私は問題がコードが製品IDを数える方法であると信じています。製品がロードされたとき(つまり、そのオプションボックスがチェックされておらず、製品がajaxを介してロードされるとき)、プロダクトIDの初期カウントのままであるため、スクリプトが調整および再計算されていないように見えます。ボックスにチェックを入れて、それを解除することで自分で試してみてください。何かご意見は? –

0

マーベラス作業革ズボン、感謝します。非常に良いチームの仕事はここでは完全な最終的なコードです。 (box.checked)場合

関数product_analysis(アドレス、ボックス){{

$('#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期間= $ 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()= 0 { VaRのtotalPriceTotal。 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); 

}

}

+0

jQueryを使用している場合、$ cを使用する代わりに$( '#GT_total_price')。 ( 'GT_total_price')関数などがあります。 jQueryを使用している場合、$ c関数は冗長です。 – nemophrost

関連する問題