我有一个购物车的项目,其中每个项目,当点击,将更新项目计数和价格。
return {
itemCount: 0,
price: 0,
itemAdd: function () {
this.itemCount += 1;
if (this.itemCount <= 5) {
this.price = 500 + (110 * this.itemCount)
} else if (this.itemCount > 5, this.itemCount <= 10) {
this.price = 1000 + (105 * this.itemCount)
} else if (this.itemCount > 10) {
this.price = 1500 + (90 * this.itemCount)
}
document.getElementById("itemPrice").innerText = this.price;
document.getElementById("itemCount").innerText = this.itemCount;
},
}
}当我点击一件商品两次时,价格和itemCount就会相应地更新。然而,当我点击一个新的项目时,itemCount和价格“重置”并从零开始。当我返回并再次点击第一项时,它继续从2开始计数。我在这里没有得到什么?
发布于 2020-11-19 21:44:39
现在更新到这个..
let price = 0;
let itemCount = 0;
const getData = () => {
return {
items: [],
itemAdd: function() {
itemCount += 1;
if (itemCount <= 5) {
price = 500 + 110 * itemCount;
} else if ((itemCount > 5, itemCount <= 10)) {
price = 1000 + 105 * itemCount;
} else if (itemCount > 10) {
price = 1500 + 90 * itemCount;
}
document.getElementById("itemPrice").innerHTML = price;
document.getElementById("itemCount").innerHTML = itemCount;
}
};
};https://stackoverflow.com/questions/64900390
复制相似问题