html实现全选与取消全选,单选

5 篇文章 0 订阅

html实现全选与取消全选,单选

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>全选与取消全选</title>
    <style>
        /* 样式可以根据需要进行调整 */
        .checkbox-container {
            display: flex;
float:left;
            flex-direction: column;
        }

        .checkbox-item {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }

        input[type="checkbox"] {
            width: 20px;
            height: 20px;
            margin-right: 5px;
        }
    </style>
</head>
<body>

    <div class="checkbox-container">
        <!-- 全选单选框 -->
        <label class="checkbox-item">
            <input type="checkbox" id="checkAll">
            全选
        </label>

        <!-- 其他单选框 -->
        <label class="checkbox-item">
            <input type="checkbox" class="individualCheckbox" value="李华">
            李华
        </label>

        <label class="checkbox-item">
            <input type="checkbox" class="individualCheckbox" value="张三">
            张三
        </label>

        <label class="checkbox-item">
            <input type="checkbox" class="individualCheckbox" value="王伟">
            王伟
        </label>

        <label class="checkbox-item">
            <input type="checkbox" class="individualCheckbox" value="赵四">
            赵四
        </label>
    </div>

    <script>
        // JavaScript代码用于实现全选与取消全选功能
        document.getElementById('checkAll').addEventListener('change', function () {
            var checkboxes = document.querySelectorAll('.individualCheckbox');
            for (var i = 0; i < checkboxes.length; i++) {
                checkboxes[i].checked = this.checked;
            }
        });
    </script>

</body>
</html>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在UniApp中实现购物车的全选单选和反选功能,可以通过以下步骤进行操作: 1. 数据结构设计:首先,你需要定义一个购物车数据结构,可以使用数组或对象来表示每个商品的信息,例如商品名称、价格、数量等。同时,为每个商品添加一个选中状态的属性,用于记录是否被选中。 2. 渲染购物车列表:在页面中使用`v-for`指令遍历购物车数据,并将商品信息展示出来。同时,为每个商品的选中状态绑定一个`v-model`指令,用于实现单选功能。 3. 实现全选功能:添加一个全选的复选,通过绑定一个变量来控制全选的状态。当全选复选被点击时,遍历购物车数据,将每个商品的选中状态与全选状态保持一致。 4. 实现反选功能:添加一个反选按钮,当点击反选按钮时,遍历购物车数据,将每个商品的选中状态取反。 5. 计算总价和总数量:通过遍历购物车数据,累加选中商品的价格和数量,得到总价和总数量。 6. 相关代码示例: ```html <template> <div> <div> <input type="checkbox" v-model="selectAll" @change="handleSelectAll" /> 全选 <button @click="handleInverseSelect">反选</button> </div> <div v-for="(item, index) in cartList" :key="index"> <input type="checkbox" v-model="item.selected" @change="handleSelectItem(index)" /> {{ item.name }} - ¥{{ item.price }} - 数量:{{ item.quantity }} </div> <div> 总价:¥{{ totalPrice }},总数量:{{ totalQuantity }} </div> </div> </template> <script> export default { data() { return { selectAll: false, cartList: [ { name: '商品1', price: 10, quantity: 1, selected: false }, { name: '商品2', price: 20, quantity: 2, selected: false }, { name: '商品3', price: 30, quantity: 3, selected: false } ] }; }, computed: { totalPrice() { let total = 0; for (let item of this.cartList) { if (item.selected) { total += item.price * item.quantity; } } return total; }, totalQuantity() { let total = 0; for (let item of this.cartList) { if (item.selected) { total += item.quantity; } } return total; } }, methods: { handleSelectAll() { for (let item of this.cartList) { item.selected = this.selectAll; } }, handleSelectItem(index) { this.cartList[index].selected = !this.cartList[index].selected; }, handleInverseSelect() { for (let item of this.cartList) { item.selected = !item.selected; } } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

骨子里的偏爱

上传不易,还请多多支持。

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

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

打赏作者

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

抵扣说明:

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

余额充值