使用Javascript和JSON添加和减去

我有一个T恤价格forms,你可以选择服装,颜色,添加多个位置,然后一些额外的选项。

我试图找出如何制作它,当你选择“添加第二次打印,它增加了衬衫的价格,但也增加了价格折扣,如72pc,96pc,144pc等等。

我尝试了很多东西。 我想我可能需要一个if语句,所以无论何时选择第二个打印选项,它都会增加价格,然后对每件作品应用折扣,但我似乎无法弄明白。

我会在底部展示一些我的代码,但你可以在https://gist.github.com/1719315查看整个代码。

谢谢你的关注!

Heres是我对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 } }, 

inheritance人计算:

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

首先,我建议做计算服务器端。 您可以使用一个简单的Web服务,其中一个方法将衬衫的数量作为参数。

如果您不使用WCF服务,则可以使用此库反序列化JSON参数:

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

进行计算并将其发送回客户端。 您可以使用jQuery轻松完成此操作,如下所述: http : //api.jquery.com/jQuery.post/

在成功回调中,您可以操纵DOM以显示正确的信息。