最垃圾的方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<table border=1 align="center" width="40%">
<tr align="center">
<th>
商品名称
</th>
<th>
商品图片
</th>
<th>
商品单价
</th>
<th>
数量商品
</th>
</tr>
<tr align="center">
<td>尿不湿</td>
<td>
<img :src="imgurl1" alt="" width="50px">
</td>
<td>{{price1}}</td>
<td>
<button @click="add1">+</button>
<input type="text" id="nbs" size="1" :value="value1">
<button @click="dele1">-</button>
</td>
</tr>
<tr align="center">
<td>酱油</td>
<td>
<img v-bind:src="imgurl2" alt="" width="50px">
</td>
<td>{{price2}}</td>
<td>
<button @click="add2">+</button>
<input type="text" id="nbs" size="1" :value="value2">
<button @click="dele2">-</button>
</td>
</tr>
<tr align="center">
<td>毛衣</td>
<td>
<img :src="imgurl3" alt="" width="50px">
</td>
<td>{{price3}}</td>
<td>
<button @click="add3">+</button>
<input type="text" id="nbs" size="1" :value="value3">
<button @click="dele3">-</button>
</td>
</tr>
<tr>
<td colspan="4" align="right">
总价: <input type="text" id="zj" size="5" :value="all">
</td>
</tr>
</table>
</div>
<script>
const app=new Vue({
el:"#app",
data:{
imgurl1:"icon/nbs.png",
imgurl2:"icon/jy.png",
imgurl3:"icon/my.png",
value1:0,
value2:0,
value3:0,
all:0,
price1:15,
price2:13,
price3:42
},
methods:{
add1:function(){
this.value1= this.value1+1,
this.all+=this.price1
},
add2:function(){
this.value2= this.value2+1,
this.all+=this.price2
},
add3:function(){
this.value3= this.value3+1,
this.all+=this.price3
},
dele1:function(){
if(this.value1>=1){
this.value1= this.value1-1,
this.all-=this.price1
}
},
dele2:function(){
if(this.value2>=1){
this.value2= this.value2-1,
this.all-=this.price2
}
},
dele3:function(){
if(this.value3>=1){
this.value3= this.value3-1,
this.all-=this.price3
}
}
}
})
</script>
</body>
</html>
略有改进
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<table border=1 align="center" width="40%">
<tr align="center">
<th>
商品名称
</th>
<th>
商品图片
</th>
<th>
商品单价
</th>
<th>
数量商品
</th>
</tr>
<tr v-for="item in items" align="center">
<td>
{{item.name}}
</td>
<td>
<img v-bind:src="item.img" alt="" width="50px">
</td>
<td>
{{item.danjia}}
</td>
<td>
<button @click="item.number+=1">+</button>
{{item.number}}
<button @click="item.number<=1 ? item.number=0 : item.number-=1">-</button>
</td>
</tr>
<tr align="right">
<td colspan="4">
总价: {{alll}}
</td>
</tr>
</table>
</div>
<script>
const app=new Vue({
el:"#app",
data:{
all:0,
items:[
{name:"尿不湿",img:"icon/nbs.png",danjia:15,number:0},
{name:"酱油",img:"icon/jy.png",danjia:6,number:0},
{name:"毛衣",img:"icon/my.png",danjia:45,number:0}
]
},
computed: {
alll:function(){
let num=0;
for (let i = 0; i <this.items.length; i++) {
num+=this.items[i].number*this.items[i].danjia;
}
return num;
}
},
})
</script>
</body>
</html>