[微信小程序]实现列表点赞并让当前节点的图片改变,值加1完整实例代码加GIF图

 //wxml
 <block wx:for="{{msg}}">
 <image  wx:if="{{item.is_say_yes ==false}}" bindtap='zan' data-id='{{item.id}}' src='../../image/zan1.png'></image>
            <image  wx:if="{{item.is_say_yes ==true}}" bindtap='zan' data-id='{{item.id}}' src='../../image/zan2.png'></image>
            <text> {{item.num_say_yes}}</text>
 </block>
//js
data:{
//'is_say_yes':false 的意思是默认没有点赞  num_say_yes参数是点赞数
	msg:[{'a':1,'is_say_yes':false,'num_say_yes':0},
	     {'b':2,'is_say_yes':false,'num_say_yes':0}
	    ]
}
zan: function (e) {
    var info_id = e.currentTarget.dataset.id;
    var that = this;
    var url = util.apiUrl + 'FWinfo/give_like';
    util.request(url, 'post', { 'program_id': app.program_id, 'info_id': info_id, 'openid': app.openid }, '正在加载数据', function (res) {
        if (res.data.state == 1) {
          wx.showToast({
            title: '点赞成功',
            icon: 'success',
            duration: 2000
          })
          for (var i = 1; i < that.data.msg.length; i++) {
            if (that.data.msg[i].id == info_id) {
              // 点赞成功时遍历msg对象并获取到当前节点的id
              that.setData({
		// 改变msg对象 i 节点的 is_say_yes 的值为true,num_say_yes值等于当前的num_say_yes加一
                ['msg[' + i + '].is_say_yes']: true,
                ['msg[' + i + '].num_say_yes']: parseInt(that.data.msg[i].num_say_yes) + 1
              })
            }
          }
        } else if (res.data.state == -1) {
          wx.showToast({
            title: '操作失败',
            image: '../../image/false.png',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '已点赞',
            icon: 'success',
            duration: 2000
          })
        }
      
      
    })
  },

微信小程序开发交流qq群   581478349

   承接微信小程序开发。扫码加微信。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

a_靖

对你有帮助吗?打赏鼓励一下?

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

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

打赏作者

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

抵扣说明:

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

余额充值