0

我需要在此脚本中将 OptionPricing 变量添加到 var AccompPricing 中的 2: 258。我想写这样的东西 2: 258 + (var Price),但它不是那样工作的。总包应添加到第二个选择值。示例:258 + 包裹价格 = Total #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); });
4

1 回答 1

1

听起来您需要在AccompPricing对象中的条目上设置一个标志,以告诉您是否再次添加包裹价格。像这样的东西:Live Copy | 直播源

// 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 中使用初始上限变量名来表示全局变量是非常不寻常的。

于 2013-06-13T07:22:57.057 回答