【购物车全选案例】(html,css,javascript)

<body>
    <input type="checkbox" id="all">全选
    <hr>
    <ul class="shop">
        <li>
            <input type="checkbox">商品1
        </li>
        <li>
            <input type="checkbox">商品2
        </li>
        <li>
            <input type="checkbox">商品3
        </li>
        <li>
            <input type="checkbox">商品4
        </li>
    </ul>
    <script>
        var oAll=document.getElementById("all")
        var oitems=document.querySelectorAll(".shop input")
        //全选
        oAll.onclick=function(){
            for( var i=0;i<oitems.length;i++){
                oitems[i].checked=oAll.checked
            }
        }
        //
        for(var i=0;i<oitems.length;i++){
            oitems[i].onclick=function(){
                count=0
                //for判断每一项,如果checked count++
                for(var i=0;i<oitems.length;i++){
                    if(oitems[i].checked) count++
                }
                if(count===oitems.length){
                    oAll.checked=true
                }else{
                    oAll.checked=false
                }
            }
        }
    </script>
</body>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是uniapp购物车全选的示例代码,可以根据自己的需求进行修改: HTML代码: ```html <template> <view> <view v-if="cartList.length === 0">购物车为空</view> <view v-else> <view v-for="(item, index) in cartList" :key="index"> <view class="cart-item"> <view class="checkbox" @click="selectItem(index)"> <image :src="item.selected ? './images/checkbox-checked.png' : './images/checkbox.png'"></image> </view> <view class="item-info">{{ item.name }}</view> </view> </view> <view class="cart-footer"> <view class="checkbox" @click="selectAll"> <image :src="isAllSelected ? './images/checkbox-checked.png' : './images/checkbox.png'"></image> </view> <view>全选</view> </view> </view> </view> </template> ``` JavaScript代码: ```js <script> export default { data() { return { cartList: [ { id: 1, name: '商品1', selected: false }, { id: 2, name: '商品2', selected: false }, { id: 3, name: '商品3', selected: false }, { id: 4, name: '商品4', selected: false }, ] } }, computed: { isAllSelected() { return this.cartList.every(item => item.selected) } }, methods: { selectItem(index) { this.cartList[index].selected = !this.cartList[index].selected }, selectAll() { this.cartList.forEach(item => item.selected = !this.isAllSelected) } } } </script> ``` CSS代码: ```css <style> .cart-item { display: flex; align-items: center; margin-bottom: 10px; } .checkbox { width: 30px; height: 30px; margin-right: 10px; display: flex; align-items: center; justify-content: center; } .item-info { flex: 1; } .cart-footer { display: flex; align-items: center; margin-top: 10px; } .cart-footer .checkbox { margin-right: 10px; } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王猫粮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值