2012-02-09 18 views
2

私は、衣服、色、複数の場所を追加して追加オプションを選択できるTシャツの価格表を用意しています。JavascriptとJSONの加算と減算

イムは、あなたが「それはシャツに価格を追加するだけでなく、その上72pc、96pc、144pcのような量あたりの価格割引を追加し、第二の印刷の追加を選択したときに、それを作る方法を把握しようとしている。

私は複数のものを試しました。私はifステートメントが必要なので、2番目の印刷オプションを選択すると価格が追加されますが、1枚あたりに値引きが適用されますが、わかりません。

I一番下に私のコードを少し表示しますが、全体を見ることができますhttps://gist.github.com/1719315

ありがとうそれを見て!

相続人は、JSONのための私の選択肢の一つです:

var shirtsJSON=[ 
{ 
    "pattern": "Delta Adult S/S 5.2oz", 
    "basePrice": 4.26, 
    "colors": { 
     "white": 0, 
     "athletic": 0.12, 
     "color": 0.23 
    }, 

    "deductions": { 
     "de48pp": 0, 
     "de72pp": 0.68, 
     "de96pp": 1.01, 
     "de144pp": 1.45, 
     "de288pp": 1.69, 
     "de576pp": 1.91, 
     "de1200pp": 1.98, 
     "de1800pp": 1.98, 
     "de2400pp": 1.98, 
     "de5000pp": 1.98 
    }, 
    "oneLocation": { 
     "onelocnone": 0, 
     "oneloc12": 3.28, 
     "oneloc34": 5.41, 
     "oneloc56": 7.52, 
     "oneloc78": 9.69, 
     "oneloc910": 11.80 
    }, 
    "twoLocation": { 
     "twolocnone": 0, 
     "twoloc12": 3.28, 
     "twoloc34": 5.41, 
     "twoloc56": 7.52, 
     "twoloc78": 9.69, 
     "twoloc910": 11.80 
    }, 
    "threeLocation": { 
     "threelocnone": 0, 
     "threeloc12": 3.28, 
     "threeloc34": 5.41, 
     "threeloc56": 7.52, 
     "threeloc78": 9.69, 
     "threeloc910": 11.80 
    }, 
    "fourLocation": { 
     "fourlocnone": 0, 
     "fourloc12": 3.28, 
     "fourloc34": 5.41, 
     "fourloc56": 7.52, 
     "fourloc78": 9.69, 
     "fourloc910": 11.80 
    } 
}, 

それは計算HERESに:すべての

 function calculatePrices() { 
      totalPrice = 0.00; 
      for (i = 1; i <= numShirts; i++) { 
       sIndex = parseInt($('#shirt' + i + 'pattern').val()); 
       color = $('#shirt' + i + 'color').val(); 
       oneLocation = $('#shirt' + i + 'oneLocation').val(); 
       twoLocation = $('#shirt' + i + 'twoLocation').val(); 
       threeLocation = $('#shirt' + i + 'threeLocation').val(); 
       fourLocation = $('#shirt' + i + 'fourLocation').val(); 
       deductions = $('#shirt' + i - 'deductions').val(); 

       price = shirtsJSON[sIndex]["basePrice"]; 
       price += shirtsJSON[sIndex]["colors"][color]; 
       price += shirtsJSON[sIndex]["oneLocation"][oneLocation]; 
       price += shirtsJSON[sIndex]["twoLocation"][twoLocation]; 
       price += shirtsJSON[sIndex]["threeLocation"][threeLocation]; 
       price += shirtsJSON[sIndex]["fourLocation"][fourLocation]; 

       totalPrice += price; 
      } 

      $('#48pc').html("<u>48pc : </u>" + currency + getMoney(totalPrice * 48)); 
      $('#72pc').html("<u>72pc : </u>" + currency + getMoney(totalPrice * 72)); 
      $('#96pc').html("<u>96pc : </u>" + currency + getMoney(totalPrice * 96)); 
      $('#144pc').html("<u>144pc : </u>" + currency + getMoney(totalPrice * 144)); 
      $('#288pc').html("<u>288pc : </u>" + currency + getMoney(totalPrice * 288)); 
      $('#576pc').html("<u>576pc : </u>" + currency + getMoney(totalPrice * 576)); 
      $('#1200pc').html("<u>1200pc : </u>" + currency + getMoney(totalPrice * 1200)); 
      $('#1800pc').html("<u>1800pc : </u>" + currency + getMoney(totalPrice * 1800)); 
      $('#2400pc').html("<u>2400pc : </u>" + currency + getMoney(totalPrice * 1800)); 
      $('#5000pc').html("<u>5000pc : </u>" + currency + getMoney(totalPrice * 5000)); 


      $('#48pp').html("<u>Per Piece : </u>" + currency + getMoney((totalPrice - shirtsJSON[sIndex].deductions.de48pp)/(1-0.25))); 
      $('#72pp').html("<u>Per Piece : </u>" + currency + getMoney((totalPrice - shirtsJSON[sIndex].deductions.de72pp)/(1-0.25))); 
      $('#96pp').html("<u>Per Piece : </u>" + currency + getMoney((totalPrice - shirtsJSON[sIndex].deductions.de96pp)/(1-0.25))); 
      $('#144pp').html("<u>Per Piece : </u>" + currency + getMoney((totalPrice - shirtsJSON[sIndex].deductions.de144pp)/(1-0.15))); 
      $('#288pp').html("<u>Per Piece : </u>" + currency + getMoney((totalPrice - shirtsJSON[sIndex].deductions.de288pp)/(1-0.15))); 
      $('#576pp').html("<u>Per Piece : </u>" + currency + getMoney((totalPrice - shirtsJSON[sIndex].deductions.de576pp)/(1-0.12))); 
      $('#1200pp').html("<u>Per Piece : </u>" + currency + getMoney((totalPrice - shirtsJSON[sIndex].deductions.de1200pp)/(1-0.12))); 
      $('#1800pp').html("<u>Per Piece : </u>" + currency + getMoney((totalPrice - shirtsJSON[sIndex].deductions.de1800pp)/(1-0.12))); 
      $('#2400pp').html("<u>Per Piece : </u>" + currency + getMoney((totalPrice - shirtsJSON[sIndex].deductions.de2400pp)/(1-0.12))); 
      $('#5000pp').html("<u>Per Piece : </u>" + currency + getMoney((totalPrice - shirtsJSON[sIndex].deductions.de5000pp)/(1-0.10))); 

答えて

1

まず、私は計算サーバサイドを行うことをお勧めします。あなたは引数としてシャツの数を取る1つの方法で簡単なWebサービスを使用することができます。

あなたはWCFサービスを使用しない場合は、このライブラリをJSON引数をデシリアライズすることができます

http://james.newtonking.com/pages/json-net.aspx

は計算を行い、それをクライアントに送り返します。 http://api.jquery.com/jQuery.post/

成功コールバックの中で、正しい情報を表示するためにDOMを操作できます。