vue中单选框,利用不存在的值标示选中状态

1、效果预览

 

 

2、index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0">
    <title>Title</title>
    <link rel="stylesheet" href="css/index.css">
</head>
<body>
<div id="app">
<!--<h2>{{title}}</h2>-->
    <li v-for="(item,index) in productList">
        <div >产品名称:{{item.productName}}</div>
        <a href="javascript:;" class="item-check-btn" v-bind:class="{'check':item.checked}" @click="selectedProduct(item);">
        </a>
    </li>

</div>
<script src="js/lib/vue.min.js"></script>
<script src="js/lib/vue-resource.min.js"></script>
<script src="js/cart.js"></script>
</body>
</html>

3、cart.js

/**
 * Created by kk on 2017/4/16.
 */
new Vue({
    el:"#app",
    data:{
        // title:"hello vue"
        totalMoney:0,
        productList:[]
    },
    filters:{
formatMoney:function (value) {
    return "¥"+value.toFixed(2)
}
    },
    mounted:function () {
        //类似于jquery中的ready方法
        this.$nextTick(function () {
            this.cartView();
        })

    },
    methods:{
        selectedProduct:function (item) {
            //alert("1");
            if(typeof item.checked=="undefined"){
                //Vue.set(item,"checked",true);//全局注册checked
                this.$set(item,"checked",true);//局部注册checked
            }
            else {
                item.checked=!item.checked;
            }
        }
    }

});
Vue.filter("money",function (value,type) {
    return "¥"+value.toFixed(2)+type;
});

4、index.css

img {
    width: 50px;
}
a {
    font-size: 30px;
    color: #000;
    text-decoration: none;
}
.check{
    background: #EE7A23;
    border-color: #EE7A23;

}
.item-check-btn {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 1px solid #ccc;
    border-radius: 50%;
    text-align: center;
    vertical-align: middle;
    cursor: pointer;
}

 

转载于:https://www.cnblogs.com/hongmaju/p/6728316.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值