我为按钮设置了ajax功能,如果我点击它就意味着'喜欢'并记录到数据库中
然后按钮会变为'不喜欢',如果再次点击则表示删除记录。
在记录部分工作正常,但我坚持成功功能
这是我的整个javascript ..
<script type="text/javascript">
function addLikes(userFRIEND,action) {
$('.demo-tutor #tutorial-'+userFRIEND+' li').each(function(index) {
$(this).addClass('selected');
$('#tutorial-'+userFRIEND+' #rating').val((index+1));
if(index == $('.demo-tutor #tutorial-'+userFRIEND+' li').index(obj)) {
return false;
}
});
$.ajax({
url: "add_follow.php",
data:'userFRIEND='+userFRIEND+'&action='+action,
type: "POST",
beforeSend: function(){
$('#tutorial-'+userFRIEND+' .btn-likes').html("<img src='http://www.seedsofpeace.org/gif/loading.gif' />");
},
success: function(data){
var followings = parseInt($('#likes-'+userFRIEND).val());
switch(action) {
case "like":
$('#tutorial-'+userFRIEND+' .btn-likes').html(' <button type="button" class="btn btn-danger btn-xs unlike" onClick="addLikes('+userFRIEND+',\'unlike\')"> Unfollow </button>');
followings = followings+1;
break;
case "unlike":
$('#tutorial-'+userFRIEND+' .btn-likes').html(' <button type="button" class="btn btn-danger btn-xs like" onClick="addLikes('+userFRIEND+',\'like\')"> Follow </button>')
followings = followings-1;
break;
}
$('#likes-'+userFRIEND).val(followings);
if(followings>0) {
$('#tutorial-'+userFRIEND+' .label-likes').html(followings);
} else {
$('#tutorial-'+userFRIEND+' .label-likes').html('0');
}
}
});
}
</script>
我尝试对成功功能发出警告,这是有效的。 所以我认为错误必须在
上$('#tutorial-'+userFRIEND+' .btn-likes').html("blabla");
#tutorial - '+ userFRIEND +'。btn-likes
但说实话,我不知道如何修理它
这是与javascript相关的html (我将通过图片显示以便于查看。)
非常感谢你的每一个建议。
答案 0 :(得分:0)
似乎你对发布的数据做错了,试试这种格式:
url: "add_follow.php",
data: {
userFRIEND:userFRIEND,
action:action
},
type: "POST",
然后尝试这个选择器:
console.log(userFRIEND) // this really logs "1" ?
$('#tutorial-'+userFRIEND+').find('.btn-likes').html("blabla");
答案 1 :(得分:0)
好像你在没有
的情况下使用jquery$(document).ready(function() {
// your code
});
一定要把js代码放在页面的末尾,如前所述,把对象放在ajax数据属性中