我正在编写一个简单的购物车来处理用户输入,然后再将金额转交给我的支付处理器。我有有效的代码,但我不确定它是否是计算总数的最有效方法。其次,我想在选择三个类别时添加百分比折扣的可能性。我最初有一种方法可以对 IF 语句进行大量检查,但效率低下,并且还有一个未解决的问题。我将如何对我预先存在的代码应用百分比折扣(如果四个项目类别中的三个> 0)?
var subtotal = 0;
var veu4 = 0;
var veo4 = 0;
var vres = 0;
var vcvl = 0;
var vedb = 0;
function update_price(pin) {
quantity = parseFloat(pin.value);
var callname = pin.name;
if (callname == "item1"){
price = quantity * 50;
subtotal -= vcvl * 50;
vcvl = quantity;
}
else if (callname == "item2"){
price = quantity * 50;
subtotal -= vres * 50;
vres = quantity;
}
else if (callname == "item3"){
price = quantity * 99;
subtotal -= veu4 * 99;
veu4 = quantity;
}
else if (callname == "item4"){
price = quantity * 129;
subtotal -= veo4 * 129;
veo4 = quantity;
}
else{
//commented out irrelevant
}
subtotal += price;
passtotal = document.getElementById("ftotal");
total = document.getElementById("ptotal");
total.innerHTML = subtotal;
passtotal.value = subtotal;
passtotal.innerHTML = subtotal;
}
}
非常感谢您的帮助!