使用一次后重新绑定AJAX中的Click事件

时间:2013-08-20 22:38:16

标签: php javascript jquery mysql ajax

我在AJAX调用上遇到点击事件时遇到问题。我嵌套了AJAX调用,因为click事件发生在第一个AJAX调用之前不存在的div上。基本上我是从数据库加载用户注释,然后评论上有一个上下投票按钮。这是第二次ajax调用。你可以在这里看到JS:

//FIRST LOAD THE COMMENTS WITH THE FIRST AJAX CALL
$('.comments_submit').each(function(){
    var comments_text = $(this).parent().find('textarea').val();
    var video_id = $(this).closest('div.home_video').find('.video').attr('id');

    var myData = 'body=' + comments_text + '&video_id=' + video_id + '&type=load';
    var that = this;

    $.ajax({
                type: 'POST', // HTTP method POST or GET
                url: 'comments_ajax.php', //Where to make Ajax calls
                dataType:'text', // Data type, HTML, json etc.
                data:myData, //post variables
                success:function(response){

                //UPON SUCCESSFULLY LOADING THE COMMENTS / BIND CLICK EVENT TO VOTE UP AND VOTE DOWN
                    $(that).closest('div.home_video').find('.comments_list').html(response);

                    $('.vote_link.down').each(function(){
                        $(this).click(function(){
                            var comment_id = $(this).closest('.comment_box').attr('id');

                            //MAKE AJAX CALL TO SUBMIT COMMENTS

                            var myData = 'comment_id=' + comment_id + '&type=vote_down';
                            var that = this;

                            $.ajax({
                                type: 'POST', // HTTP method POST or GET
                                url: 'comments_ajax.php', //Where to make Ajax calls
                                dataType:'text', // Data type, HTML, json etc.
                                data:myData, //post variables
                                success:function(response){

                                //REFORMAT UPON SUCCESSFUL AJAX CALL
                                $(that).text(response);

                                },
                                error:function (xhr, ajaxOptions, thrownError){
                                    alert('didnt work'); //throw any errors
                                }
                            });
                        });
                    });


                    $('.vote_link.up').each(function(){
                        $(this).click(function(){
                            var comment_id = $(this).closest('.comment_box').attr('id');

                            //MAKE AJAX CALL TO SUBMIT COMMENTS

                            var myData = 'comment_id=' + comment_id + '&type=vote_up';
                            var that = this;

                            $.ajax({
                                type: 'POST', // HTTP method POST or GET
                                url: 'comments_ajax.php', //Where to make Ajax calls
                                dataType:'text', // Data type, HTML, json etc.
                                data:myData, //post variables
                                success:function(response){

                                //REFORMAT UPON SUCCESSFUL AJAX CALL
                                $(that).text(response);

                                },
                                error:function (xhr, ajaxOptions, thrownError){
                                    alert('didnt work'); //throw any errors
                                }
                            });
                        });
                    });



                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert('didnt work'); //throw any errors
                }
        });
});

评论加载的服务器端代码是:

if($_POST['type']=="load"){
$sql_select = "SELECT * FROM comments WHERE video_id = '$video_id' ORDER BY timestamp DESC LIMIT 7";
$result_select = $mysqli->query($sql_select);

while($row = mysqli_fetch_array($result_select)){
    echo "<div class='comment_box' id='".$row['id']."'>".$row['body']."
        <div class='votes'>
            <a class='vote_link up'>UP (".$row['vote_up'].")</a>
            <a class='vote_link down'>DOWN (".$row['vote_down'].")</a>
        </div>
    </div>";
}

}

投票的服务器端代码(与投票代码完全相同):

if($_POST['type']=="vote_up"){
$comment_id = $_POST['comment_id'];

$sql_select = "SELECT * FROM comments WHERE id = '$comment_id'";
$result_select = $mysqli->query($sql_select);
$row = mysqli_fetch_array($result_select);
$votes = $row['vote_up'] + 1;

$sql_update = "UPDATE comments SET vote_up = '$votes' WHERE id = '$comment_id'";
$result_update = $mysqli->query($sql_update);

$sql_select = "SELECT * FROM comments WHERE id = '$comment_id'";
$result_select = $mysqli->query($sql_select);
$row_vote = mysqli_fetch_array($result_select);

echo "
UP (".$row_vote['vote_up'].")
";

}

一些事情。单击按钮时,由于某种原因,它会向UP或DOWN字段添加多个投票。但它也会取消绑定点击事件。

我从来没有嵌套过AJAX调用,所以也许在第一个函数发生后有更好的方法来绑定AJAX函数?谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

你应该可以这样做:

$('.vote_link.down').click(function(){
    var comment_id = $(this).closest('.comment_box').attr('id');
    // etc.
});

$('.vote_link.up').click(function(){
    var comment_id = $(this).closest('.comment_box').attr('id');
    // etc.
});

这应该简化您的代码,并可能解决您的一些问题。

答案 1 :(得分:0)

我找到了答案(对于遇到同样问题的其他人)。而不是在顶级AJAX调用中绑定函数,只需使用$(document).on()来调用函数,如:

$(document).on("click",".vote_link.down",function(e){
     alert('testing'); 
 });