vue.js如何实现购物车加减操作


效果图

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/css.css">
    <script src="js/vue.js"></script>
    <title>Vue</title>
</head>

<body>
    <div id="app">
        <div class="hd">
            <span>
                <input type="checkbox" id="selectAll" v-model="cheackValue" />
                <label for="selectAll">全选</label>
            </span>
            <span>商品名称</span>
            <span>单价</span>
            <span>数量</span>
            <span>小计</span>
            <span>操作</span>
        </div>
        <div class="goods-list">
            <div class="check-btn"><input type="checkbox"></div>
            <div class="goods-img"><img src="img/canon.jpg" alt=""></div>
            <div class="goods-name"><em>AOC 23英寸IPS显示器</em></div>
            <div class="goods-price"><em>¥150.00</em></div>
            <div class="goods-quantity">

                <button v-on:click="subtract()">-</button>
                <input type="text" value="0" v-model="count">
                <button v-on:click="add()">+</button>

            </div>
            <div class="goods-subtotal"><em>¥0.00</em></div>
            <div class="goods-operate"><a href="javascript:void(0)">删除</a></div>
        </div>
        <div class="clearing">
            <span><a href="javascript:void(0)">删除选中的商品</a></span>
            <span>已选择<em>1</em>件商品</span>
            <span>总价:¥<em>0.00</em></span>
            <span><a href="#">去结算</a></span>
        </div>
    </div>
</body>

</html>
<script>
        var vm = new Vue({
            el: "#app",
            data: {
                count: 0
            },
            methods: {
            //实现数字增加
                add: function(count) {
                    this.count++;
                },
                //数字减少,如果少于0则弹出不能再少
                subtract: function(count) {
                    if (this.count <= 0) {
                        alert("不能少了")
                    } else {
                        this.count -= 1;

                    }
                }

            }
        });

    </script>
Logo

前往低代码交流专区

更多推荐