简单的购物车商品价格计算

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script>
<style>

</style>
</head>
<body>
<div id="app">
<div class="li">商品价格:<input placeholder="输入产品价格" type="number" v-model="value" /></div>
<div class="li">
商品数量:<input value="-" type="button" @click="minusNum" /> <span class="num">{{number}}</span> <input value="+" type="button" @click="increaseNum" />
</div>
<div class="li">
总价格:<span class="price">{{allPrice}}</span>
</div>
</div>
<script>
var vm = new Vue({
el:'#app',
data:{
value:"0",
number:"0",
price:"0"
},
computed:{
allPrice:function(){
return this.price = this.number * this.value;
}
},
methods:{
minusNum:function(){
if(this.number>0){
this.number --;
}
else{
this.number = 0;
alert("不能更少了")
}

},
increaseNum:function(){
this.number ++;
}
}
})
</script>
</body>
</html>

posted on 2020-11-10 11:25  caicai2015  阅读(126)  评论(0编辑  收藏  举报

导航