我正在尝试将这些数字添加在一起作为汇总值,具体取决于选中的项目以在我的结帐页面上显示为总计。
我已经尝试过
if ( this.products[0].selected === true ) {
this.summary = this.products[0].price
} else if ( this.products[0].selected === true, this.products[1].selected === true ) {
this.summary = this.products[0].price + this.products[1].price
} else {
this.summary = 0;
}
显然没有完整的声明,但到目前为止它甚至没有起作用。
我有一个包含此的对象数组
data: {
summary: 0,
products: [
{ name: 'Hemsida', price: 290, selected: false },
{ name: 'Copywriting', price: 190, selected: false },
{ name: 'Fotografering', price: 190, selected: false }
]
}
当我选中我的框时,它会将链接变为!products[0].selected
我最近的尝试是使用 switch 语句,我可能已经完全放弃了它,因为这是我在该领域的第一个 switch 语句:P
priceSummary() {
switch (
(this.products[0].selected,
this.products[1].selected,
this.products[2].selected)
) {
case (true, false, false):
this.summary = this.products[0].price
break
case (true, true, false):
this.summary = this.products[0].price + this.products[1].price
break
case (true, false, true):
this.summary = this.products[0].price + this.products[2].price
break
case (false, false, true):
this.summary = this.products[2].price
break
case (false, true, false):
this.summary = this.products[1].price
break
case (false, false, false):
this.summary = 0
break
case (true, true, true):
this.summary =
this.products[0].price +
this.products[1].price +
this.products[2].price
default:
this.summary = 0
}
它做了一些事情,但它没有做它应该做的事情:P到处都是。帮助任何人???
最佳答案
类似的事情:
//looping on products
this.product.forEach((prod) => {
//checking if product is selected
if(prod.selected === true){
// if it's selected, adding its price to the sum
this.summary += prod.price
}
}
?
关于javascript - 如何根据复选框汇总值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55263107/