qq_37970469 2023-04-24 12:02 采纳率: 0%
浏览 43

关于vant-checkbox-group无法勾选的问题

//cart.wxml
<van-checkbox-group bindchange="onChange">
  <view wx:for="{{carts}}" wx:key="_id" wx:for-item="item">
    <van-checkbox value="{{item.checked}}" data-id="{{item._id}}">
      <van-card thumb="{{item.imageSrc}}" title="{{item.title}}" desc="{{item.desc}}" price="{{ item.price }}">
        <view slot="bottom">
          <view class="goods-r-bottom">
            <van-button bind:click="subCount" data-id="{{item._id}}" round="true" size="mini">-</van-button>
            <view class="goods-r-bottom-text">{{item.num}}</view>
            <van-button bind:click="addCount" data-id="{{item._id}}" round="true" size="mini">+</van-button>
          </view>
        </view>
      </van-card>
    </van-checkbox>
  </view>
</van-checkbox-group>

<van-submit-bar price="{{totalPrice}}" button-text="提交订单" bind:submit="startpay">
  <van-checkbox value="{{allChecked}}" clickable bind:change="onAllChange">全选</van-checkbox>
</van-submit-bar>
 //cart.js
 //处理购物车中商品勾选的变化
  onChange(event) {
    // 获取选中的复选框值
    const checkedValues = event.detail.value;
    console.log('event:', event);
    
    // const { value: checkedValues } = event.detail;
    // console.log('event:', event);

    // 复制购物车数组
    const carts = [...this.data.carts];

    // 更新每个项目的checked属性
    for (let i = 0; i < carts.length; i++) {
      const index = carts[i]._id;
      carts[i].checked = checkedValues.includes(index);
    }

    // 检查是否所有商品都已勾选
    const allChecked = carts.every(item => item.checked);

    // 计算总价
    const totalPrice = carts.filter(item => item.checked).reduce((acc, cur) => acc + cur.price, 0);

    // 更新数据
    this.setData({
      carts,
      allChecked,
      totalPrice
    });
  },

//勾选全选
  onAllChange(event) {
    const allChecked = event.detail.length > 0;
    const carts = [...this.data.carts].map(item => ({
      ...item,
      checked: allChecked
    }));
    const totalPrice = allChecked ? carts.reduce((acc, cur) => acc + cur.price, 0) : 0;
    this.setData({
      carts,
      allChecked,
      totalPrice
    });
  },

想要实现vant-checkbox-group组件勾选商品卡片后统计所勾选商品总价的功能,报错

WAServiceMainContext.js:2 TypeError: Cannot read property 'detail' of undefined
    at To.onChange (cart.js:121)
    at To.onShow (cart.js:26)
    at To.<anonymous> (WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2)
    at To.i.__callPageLifeTime__ (WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2)
    at Bt (WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2)
    at WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2
    at qt (WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2)
    at r.<anonymous> (WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2)
    at ge.<anonymous> (WASubContext.js?t=wechat&s=1682298625629&v=2.23.4:2)
    at ge.emit (WAServiceMainContext.js:2)(env: Windows,mp,1.06.2303220; lib: 2.23.4)

121行是

const checkedValues = event.detail.value;

购物车内商品来自表carts,属性有_id,checked,price等,请问出了什么错,复选框无法勾选或取消勾选,全选按钮会取消全部选中但是checked属性没变,也无法勾选。

  • 写回答

2条回答 默认 最新

  • 猿小白888 2023-04-24 13:51
    关注

    把这两行调换下,先看下控制台打印的event内容

    img

    评论

报告相同问题?

问题事件

  • 创建了问题 4月24日

悬赏问题

  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库