用vue实现购物车并且当它数量减到0就不能减了!DOCTYPE htmlhtml lang=enheadmeta charset=UTF-8title统计商品总价titlestylebodyfont-family微软雅黑;font-size14pxtitlebackground #f6f6f6;font-size18px;titlecontentwidth400px;height36px;line
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>统计商品总价</title>
<style>
body{
font-family:微软雅黑;
font-size:14px}
.title{
background: #f6f6f6;
font-size:18px;
}
.title,.content{
width:400px;
height:36px;
line-height:36px;
border: 1px solid #dddddd;}
.title:not(:first-child),.content{
border-top:none;}
.title div{
width:100px;
text-align:center;
float:left}
.content{
clear:both}
.content div{
width:100px;
text-align:center;
float:left}
p{
width:380px;
text-align:right;}
#example .content:nth-child(even){
background:#E9E9E9;
}
#example .content:nth-child(odd){
background:#B0B0B0
}
.disable {
color: #ccc;
cursor: not-allowed;
}
</style>
<script src="./vue.js"></script>
</head>
<body>
<div id="example">
<div class="title">
<div>商品名称</div>
<div>单价</div>
<div>数量</div>
<div>金额</div>
</div>
<div class="content" v-for="(item,index) in shop">
<div>{{item.name}}</div>
<div>{{item.price}}</div>
<div>
<button v-bind:class="{disable:item.count<=0}" v-on:click="item.count>0?item.count--:null">-</button>
{{item.count}}
<button v-on:click="item.count++">+</button>
</div>
<div>{{item.price*item.count}}</div>
</div>
<p>合计:{{totalprice()}}</p>
</div>
<script type="text/javascript">
var exam = new Vue({
el:'#example',
data:{
<pre><code> shop : [{//定义商品信息数组
name : '女士连衣裙',
price : 499,
count : 0,
isCheckde:false,
totals:0
},{
name : '男士衬衫',
price : 399,
count : 0,
isCheckde:false,
totals:0
},
{
name : '女士短袖',
price : 128,
count : 0,
isCheckde:false,
totals:0
},
{
name : '男士短袖',
price : 138,
count : 0,
isCheckde:false,
totals:0
}
]
},
methods:{
totalprice:function(){
var t=0
for(var i=0;i<this.shop.length;i++){
t+=this.shop[i].price*this.shop[i].count
}
return t
}
}
</code></pre>
<p>})</p>
</script>
</body>
</html
原文地址: https://www.cveoy.top/t/topic/cxTB 著作权归作者所有。请勿转载和采集!