在Javascript中将返回变量添加到Number

时间:2013-06-13 06:54:24

标签: javascript variables math numbers

我需要在此脚本中将OptionPricing变量添加到2:258中,该变量位于var AccompPricing中。我想写这样的东西2:258 +(var Price),但它不能那样工作。应将总包添加到第二个选项值。示例:258 +包裹的价格=总计#2。

// Collect Data & Prices To Update Dynamic Prices

var OptionPricing = {

'pack11049': 1049,
'pack21199': 1199,
'pack31199': 1199,
'pack41299': 1299,
'pack51449': 1449,
'pack61499': 1499,
'pack71549': 1549,
'pack81699': 1699,
'pack91799': 1799,
'pack101999': 1999,
'pack112499': 2499,
'pack122549': 2549


};


function checkOptions() {

var Price = 0;

for (Packs in OptionPricing) {

    if ($('#' + Packs).is(':checked')) {           
        Price += OptionPricing[Packs];
    }
}

return Price;
}
var AccompPricing = {
0: 0,
1: 129,
2: 258 + (var Price),
3: 1057,
4: 1856    
};

function checkAccomp() {

var Accomp = parseInt($('#howmany').val(), 10);

return AccompPricing[Accomp];
}

function updateTotal() {

var ThePrice = checkOptions() + checkAccomp();


$('#TotalPrice').text('$' + ThePrice + '.00');

}

$(function () { $('.DoPricing').click(updateTotal); });

1 个答案:

答案 0 :(得分:1)

听起来您需要在AccompPricing对象的条目上添加一个标记,告诉您是否再次添加套餐价格。 像这样的东西Live Copy | Live Source

// Collect Data & Prices To Update Dynamic Prices

var OptionPricing = {

    'pack11049': 1049,
    'pack21199': 1199,
    'pack31199': 1199,
    'pack41299': 1299,
    'pack51449': 1449,
    'pack61499': 1499,
    'pack71549': 1549,
    'pack81699': 1699,
    'pack91799': 1799,
    'pack101999': 1999,
    'pack112499': 2499,
    'pack122549': 2549
};

var AccompPricing = {
    0: {Price: 0,     Flag: false},
    1: {Price: 129,   Flag: false},
    2: {Price: 258,   Flag: true},
    3: {Price: 1057,  Flag: false},
    4: {Price: 1856,  Flag: false}
};

function checkOptions() {

    var Price = 0;

    for (Packs in OptionPricing) {

        if ($('#' + Packs).is(':checked')) {           
            Price += OptionPricing[Packs];
        }
    }

    return Price;
}

function checkAccomp() {

    var Accomp = parseInt($('#howmany').val(), 10);

    return AccompPricing[Accomp];
}

function updateTotal() {

    var PackagePrice, Accomp, ThePrice;

    PackagePrice = checkOptions();
    Accomp = checkAccomp();
    ThePrice = PackagePrice + Accomp.Price;
    if (Accomp.Flag) {
        ThePrice += PackagePrice;
    }

    $('#TotalPrice').text('$' + ThePrice + '.00');
}

$(function () { $('.DoPricing').click(updateTotal); });

旁注:我已尝试在上面使用您的大写形式。仅供参考,在JavaScript中使用最初限制的变量名称除了全局变量之外,非常不常见。