data-id 的值没有绑定上无法用 e.target.dataset.id获取
发布于 5 年前 作者 qiangli 3847 次浏览 来自 问答

后台数据已经返回且是有id的,但是在页面遍历的时候,data-id却没有值,左边的商品的数据都是从后台获取的

这张图就有data-id的值,3和4 也是从后台获取到的数据

请问为什么会有这种情况 有没有大神可以指点一下

刚学小程序

5 回复

<view class=‘cartMain’>

<view>

<button bindtap=“go” data-id="{{item.id}}" class=‘cleanCart’>清空购物车</button>

</view>

<view wx:for="{{cartItems}}" wx:key>

<view data-id="{{item.id}}" class=‘cart-box’ data-index="{{index}}">

<view class=‘icon’>

<icon wx:if="{{item.selected}}" type=“success” color="#405F80" size=“20” bindtap=“selectedCart” data-index="{{index}}"/>

<icon wx:else type=“circle” bindtap=“selectedCart” size=“20” data-index="{{index}}" />

</view>

<view class=‘cart_main’>

<view class=‘left’>

<view class=‘left_imgae’>

<image class=“cart-image” src="{{item.image}}"></image>

</view>

<view class=‘left_box’>

<text class=“cart-title” hidden=‘hidden’>{{item.id}}</text>

<text class=“cart-title”>{{item.title}}</text>

<text class=“cart-price”>¥{{item.price}}</text>

<text bindtap=‘reduce’ class=“input cart-reduce” data-index="{{index}}">-</text>

<text class=“input cart-text”>{{item.value}}</text>

<text bindtap=‘add’ class=“input cart-add” data-index="{{index}}">+</text>

<text class=“input cart-stock”>库存:{{item.stock}}</text>

</view>

</view>

<view class=‘right’>

<image src=’/image/删除.png’ bindtap=‘shanchu’ data-index="{{index}}"></image>

</view>

</view>

<!-- 底部 -->

<view class=“cart-bottom”>

<icon wx:if="{{CheckAll}}" class=“cart-icon” type=“success” color="#405F80" size=“20” bindtap=“select” data-index="{{index}}" />

<icon wx:else type=“circle” class=“cart-icon” size=“20” bindtap=“select” data-index="{{index}}" />

<text class=‘checkAll’>全选</text>

<!-- <view class=‘cart-pay’>

               <text class=‘cart_pay’ bindtap=“go” data-id="{{item.id}}">清空购物车</text>

             </view> -->

<view class=‘cart-pay’>

<text class=‘cart_pay’ bindtap=“goPay” data-id="{{item.idList}}">结算</text>

</view>

<view class=‘cart-sum’>

<text class=‘cart_sum’>

<text class=‘sum_text’>合计:</text>

<text class=‘sum_color’>¥{{total}}元</text>

</text>

</view>

</view>

</view>

</view>

</view>

e.currentTarget.dataset.id就有值了

e.currentTarget

建议楼主去了解一下e.target和e.currentTarget的区别

代码片段:

回到顶部