success:function (res) { console.log(res); if(res =="error"){ console.log("请登录"); document.getElementById("collectiones").innerText = "收藏"; document.getElementById("collectiones").style.color = "black"; } if(res == "ok01"){ console.log("数据库有这条数据"); document.getElementById("collectiones").innerText = "已收藏"; document.getElementById("collectiones").style.color = "red"; alert("已经收藏"); } /* if(res = "ok"){ document.getElementById("collectiones").innerText = "收藏"; document.getElementById("collectiones").style.color = "black"; }*/ },
这里res = "ok"是一个等号,无论前面判断怎么正确,都执行res = “ok”里面的语句,
下面是正确的写法
success:function (res) { console.log(res); if(res =="error"){ console.log("请登录"); document.getElementById("collectiones").innerText = "收藏"; document.getElementById("collectiones").style.color = "black"; } if(res == "ok01"){ console.log("数据库有这条数据"); document.getElementById("collectiones").innerText = "已收藏"; document.getElementById("collectiones").style.color = "red"; alert("已经收藏"); } /* if(res == "ok"){ document.getElementById("collectiones").innerText = "收藏"; document.getElementById("collectiones").style.color = "black"; }*/ },
这里res== "ok"是两个等号