jq 全选 单选 反选

//单选checkbox
$(“input[name=actions]”).change(function () {

        var result="";
        $("input[name='actions']:checked").each(function(){
            result+=$(this).val()+',';
        });
        if(result!=""){
            result=result.substring(0,result.lastIndexOf(','));
        }
        $("#selects").val(result);
    });

//全选

    $("#checkall").click(function() {
        var result="";
        $("input[name='actions']").each(function(){
            result+=$(this).val()+',';
            $(this).prop("checked", true);
        });
        if(result!=""){
            result=result.substring(0,result.lastIndexOf(','));
        }
        $("#selects").val(result);
    });

反选
$(“#delcheckall”).click(function() {

        $("input[name='actions']").prop("checked", false);
        $("#selects").val("");

    });
在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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值