用Javascript和JSON添加和减去 [英] Adding and Subtracting with Javascript and JSON

查看:94
本文介绍了用Javascript和JSON添加和减去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个T恤价格表,您可以在其中选择服装,颜色,添加多个位置以及一些其他选项.

I have a t-shirt price form where you can select the garment, the color, add multiple locations and then some extra options.

我试图弄清楚如何在选择添加第二次打印"时进行制作,这不仅会增加衬衫的价格,而且还会增加折价幅度,例如72pc,96pc,144pc等.

Im trying to figure out how to make it when you select a "Add A Second Print it adds a price to the shirt but also adds price discount per the amount like 72pc, 96pc, 144pc and so on.

我尝试了很多事情.我想我可能需要一个if语句所以每当第二打印选项被选中它增加了价格,但随后申请折扣的每件,但我似乎无法找到答案.

I've tried multiple things. I think I might need a if statement so whenever the second print option is selected it adds the price but then apply discounts to the per piece but I cant seem to figure it out.

我将在底部显示一些代码,但是您可以在 https://gist.github.com/1719315

I'll show a little bit of my code at the bottom but you can view the whole thing at https://gist.github.com/1719315

感谢您的关注!

以下是我的JSON选项之一:

Heres is one of my options for the 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
    }
},

此处计算的地方:

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

推荐答案

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

First of all, i would recommend to do the calculation server side. You can use a simple webservice with one method that takes the number of shirts as the argument.

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

If you don't use a WCF service, you can deserialize the JSON argument with this library:

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

进行计算并将其发送回客户端.您可以按照以下说明使用jQuery轻松完成此操作: http://api.jquery.com/jQuery.post /

Do the calculation and send it back to the client. You can do this easily with jQuery as described here: http://api.jquery.com/jQuery.post/

在成功回调中,您可以然后操作DOM以显示正确的信息.

Inside the success callback you can then manipulate your DOM to display the correct information.

这篇关于用Javascript和JSON添加和减去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆