0

我在 AJAX 调用上的点击事件有问题。我有嵌套的 AJAX 调用,因为单击事件发生在第一个 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 函数?谢谢你的帮助!

4

2 回答 2

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.
});

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

于 2013-08-21T00:23:05.353 回答
0

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

$(document).on("click",".vote_link.down",function(e){
     alert('testing'); 
 });
于 2013-08-21T07:14:01.190 回答