<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>计算商品的价格</title>
</head>
<body>
<div id="app">
<label v-for="(item, index) in items" :key="index">
<input type="checkbox" :value="item.value" v-model="selectedItems">
{{ item.label }} - {{ item.price }} 元
</label>
<p>已选中的总价: {{ totalPrice }} 元</p>
</div>
</body>
<script src="./js/vue.min.js"></script>
<script src="./js/axios.min.js"></script>
<script type="text/javascript">
axios.defaults.withCredentials = true
var vm = new Vue({
el: '#app',
data: {
items: [ // 商品或服务数组
{ value: 'item1', label: '商品1', price: 100 },
{ value: 'item2', label: '商品2', price: 200 },
{ value: 'item3', label: '商品3', price: 150 },
// ... 更多商品
],
selectedItems: [] // 存储被选中的商品值的数组
},
mounted() {
},
computed: {
// 计算属性,返回已选中的商品的总价
totalPrice() {
// 使用reduce方法计算总价
return this.selectedItems.reduce((total, itemValue) => {
const item = this.items.find(item => item.value === itemValue);
if (item) {
return total + item.price;
}
return total;
}, 0);
} }
,
methods: {
}
})
</script>
</html>
html计算选中框的总价格
最新推荐文章于 2024-07-21 17:58:14 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)