第二次没有AJAX效果


No AJAX effect the second time

我目前正在使用PHP、SQL、jQuery和AJAX构建一个评级系统。

除了一个小细节外,我做得很好。当我第一次评分时,结果被成功发送到rate.php文件,保存在我的数据库中,新结果被发送回我的评分页面。但当我再次尝试评分时,什么也没发生。

甚至连jQuery的"悬停效果"都不起作用。我必须重新加载整个页面,然后才能再次评分。

我已尝试将on更改为bindclick。帮助那里。我还尝试添加了cache: false,,并将success更改为done,最后还尝试了使用content.html(result);document.getById

我是ajax的新手,所以也许有一种更简单的方法可以做到这一点。

我听说过一些关于JSON的东西,但我不知道它是否适用于此。


这是我的jQuery和AJAX网站。hp:

$(document).ready(function() {

$('span').hover(function() {
    $(this).prevAll().andSelf().toggleClass('my');
});

$('span').on('click', function() {
    // Define data to send
    var id = $(this).parent().attr("id");
    var star = $(this).attr("class");
    // Put data together and send request with ajax
    var post = "id="+id+"&stars="+star;
    $.ajax( {
        url:"includes/rate.php",
        cache: false,
        data: post,
        success:function(result) {
            document.getElementById(id).innerHTML=result;
        }
     });    
});
});

在网站上加载当前评级的PHP。hp:

http://pastie.org/private/qql0ajaq9lfjqmisgvmzkq

和价格。hp:

http://pastie.org/private/kgufwlmx69t8vxjugx9zyg

希望有人能帮我。

您正在替换元素,这将导致单击事件处理程序停止。请尝试将单击处理程序附加到文档。

更改:

$('span').on('click', function() {

收件人:

$(document).on('click', 'span', function() {

此外,在您的代码中,由于您使用jQuery,您可以更改以下内容:

document.getElementById(id).innerHTML=result;

收件人:

$('#' + id).html(result);

您应该绑定DOM中始终存在的元素。我倾向于只使用document

$(document).on('click', 'span', function(){
  // Code here
});

这样,一旦在AJAX调用中替换HTML,您就不会丢失事件