<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="boots/bootstrap-3.3.7-dist/css/bootstrap.min.css">
<script src="boots/bootstrap-3.3.7-dist/jquery-1.11.3.min.js" type="text/javascript " charset="utf-8" ></script>
<script src="boots/bootstrap-3.3.7-dist/js/bootstrap.js" type="text/javascript " charset="utf-8" ></script>
<style>
table{
text-align: center;
}
</style>
</head>
<body>
<div id="app">
<table border="" cellspacing="0" width="800px">
<tr>
<td>编号</td>
<td>名称</td>
<td>单价</td>
<td>数量</td>
<td>总价</td>
</tr>
<tr v-for="(l,index) in msg">
<td >{{l.num}}</td>
<td>{{l.name}}</td>
<td>{{l.price}}</td>
<td>
<button v-on:click="jian(index)">-</button>
{{l.you}}
<button v-on:click="alk(index)">+</button></td>
<td>{{l.price*l.you}}</td>
</tr>
</tr>
<td>{{zong}]</td>
</tr>
</table>
</div>
<script src="js/vue.js"></script>
<script>
new Vue({
el:"#app",
data:{
msg:[
{
num:"1",
name:"袜子",
price:"5",
you:"1"
},
{
num:"2",
name:"外套",
price:"180",
you:"1"
},
{
num:"3",
name:"小黑子",
price:"2",
you:"1"
},
zong="0"
]
},
methods:{
//增加
alk:function(index){
this.msg[index].you++;
var total = 0;//临时总价
this.msg.forEach(function (l,index) {
total += l.price*l.you;
});
this.zong=parseFloat(total);
},
//减少
jian:function(index){
this.msg[index].you--;
}
}
})
</script>
</body>
</html>
网友评论