当我点击 checkbox 会调用 bindchange="checkboxChange" 方法,怎么获取我每次点击的checkbox 的下标 shopIndex
因为<checkbox-group>是在循环外面且有多个checkbox 。求告知怎么获取当前点击checkbox 的下标
<checkbox-group bindchange="checkboxChange" >
<view class="row" wx:for="{{cart.cartslist}}" wx:for-item="shop"
wx:for-index="shopIndex" wx:key="shopIndex" >
<view class=" checkbox" >
<checkbox value='{{shop.id}}' checked='{{shop.checked}}' ></checkbox>
</view>
</view>
</checkbox-group>
直接在checkbox标签里传入for循环的index就可以了呀
<view class="weui-cell__hd"> <checkbox value="{{index}}" checked="{{item.checked}}"/> </view>
我也遇到了这个问题 请问 楼主 解决了吗?
checkboxChange: function(e) {
console.log(e)
let cartslist = that.data.cart.cartslist;
let checkedGoodsList = e.detail.value;
for (let i = 0; i < cartslist.length; i++) {
let shop = cartslist[i];
if (shop.status == 1) {
let good_id = shop.id.toString();
let index = checkedGoodsList.indexOf(good_id);
if (index < 0) {
shop.checked = false;
} else {
shop.checked = true;
}
}
}
that.data.cart.cartslist = cartslist
that.setData({
cart: cart,
checkedGoodsList: checkedGoodsList
})
that.countOrderTotal();
},
我是这么做的,虽然麻烦了点。但是目前还能满足需求。