jq点击收藏在此点击取消

11 篇文章 0 订阅
8 篇文章 0 订阅

点击查看案例

例如后台返回一个字段collectStatus 当status为ture显示
…/…/image/classinfo/xx.png
在这里插入图片描述
例如后台返回一个字段collectStatus 当status为false显示
…/…/image/classinfo/x.png
在这里插入图片描述

根据后台的状态前端来显示这个图片就行,

当我们再次点击的时候如果没有收藏的点击就收藏切换成收藏的图片,反之…

前端代码,根据不同的渲染模板来显示有没有被收藏的图片

if(collectStatus=='false'){
       <img src="../../image/classinfo/x.png"  id='img' alt="" onclick="collecTion(id,collectStatus)">
}else{
      <img src="../../image/classinfo/xx.png"  id='img' alt="" onclick="collecTion(id,collectStatus)">
}

collectStatus 后台返回的状态   ture 和 false

点击提交的时候需要的字段id 和 status status 1–收藏 2–取消收藏

<script>
var onOff = true;
function collecTion(id,collectStatus) {
		var oImg = document.getElementById('img');
		var urls1 = getCollection +'&cid=' + cid + '&status=' + 1; //已上传
		var urls2 = getCollection +  '&cid=' + cid + '&status=' + 2; //未上传

		if (collectStatus != 'undefined') {
			if (collectStatus == 'false') {
				if (onOff) {
					onOff = false;
					oImg.src = '../../image/classinfo/xx.png';
					ajaxGet(urls1, {}, function(ret, err) {
						api.toast({
							msg: ret.msg,
							duration: 3000,
							location: 'bottom'
						});
					})
				} else {
					onOff = true;
					oImg.src = '../../image/classinfo/x.png';
					ajaxGet(urls2, {}, function(ret, err) {
						api.toast({
							msg: ret.msg,
							duration: 3000,
							location: 'bottom'
						});
					})
				}

			} else if (collectStatus == 'true') {
				if (onOff) {
					onOff = false;
					oImg.src = '../../image/classinfo/x.png';
					ajaxGet(urls2, {}, function(ret, err) {
						api.toast({
							msg: ret.msg,
							duration: 3000,
							location: 'bottom'
						});
					})
				} else {
					onOff = true;
					oImg.src = '../../image/classinfo/xx.png';
					ajaxGet(urls1, {}, function(ret, err) {
						api.toast({
							msg: ret.msg,
							duration: 3000,
							location: 'bottom'
						});

					})
				}
			}
		}


	}
</script>	
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

1登峰造极

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值