AJAX响应不按预期附加类

时间:2013-12-30 22:41:41

标签: javascript php jquery html ajax

我正在社交网站上工作,每个用户都可以发帖,每个帖子都可以评论。当页面第一次加载时,每个帖子都会显示两条注释,其余的则被隐藏。单击Load more按钮时会隐藏隐藏的内容。每个注释都有一个类.per_comment,但是通过AJAX返回的一个包含在一个类.per_comment中,并且每个注释都将包含另一个类.per_comment。如果我使用类.per_comment将click事件应用于每个注释,默认显示的前两个工作正常,但通过AJAX返回的工作为一个,因为它们都在一个类中。

HTML:

<div class = 'rows'>
    <div class='feed_blocks'>
        <div class='feeds'>
        <!-- user post goes here -->
            <div class = 'comment_data'>
                <div class = 'per_comment'> 
                    <a href = '#'><p class = 'usernames'>username</p></a>
                    <div class = 'commenter_details'>
                        <p> commenter_full_name </p>
                    </div>
                    <p>comments..</p>
                </div>
                <div class = 'morecomments'><p> Load more </p> </div>
            </div>
        </div>
    </div
</div

jQuery AJAX:

$(".comment_data").on('click', '.morecomments', function () {
    var $this = $(this),
        $pc = $this.prev('.per_comment');
    //if the comments are already loaded then don't load it again just display it
    if ($pc.data('loaded')) {
        $this.replaceWith("<div class = 'morecomments'><p> Load more </p> </div>");  
        $pc.slideDown();
    } else {
        var post_id = $(this).val();
        var user_id = $(".user_id").text();
        var request = $.ajax({
            url: "comments.php",
            type: "POST",
            data: {
                post: post_id,
                user: user_id
            },
            dataType: "html"
        });
        request.done(function (msg) {
            $pc.html(msg).data('loaded', true);
            $this.replaceWith("<div class = 'morecomments'><p> Load more </p> </div>"); 
    });
    }
});  

PHP:comments.php

<?php
require_once('../connection.php');

if(isset($_POST['post']) && isset($_POST['user'])) 
{
$post_id = $_POST['post']; 
$user_id = $_POST['user']; 
$com = "SELECT a, b, c FROM abc ORDER BY time ASC LIMIT 2, 1000";
    $q = $conn->prepare($com) or die("ERROR: " . implode(":", $conn->errorInfo()));
    $q->bindParam(1, $post_id);
    $q->execute();
    if($commentz = $q->fetchAll()){
     foreach ($commentz as $comment){

        echo "<div class = 'per_comment'>";
            echo "<a href = '#'><p class = 'usernames'> commenter_name  </p></a>";
            echo "<div class = 'commenter_details'>";
                echo "<p> commenter_full_name </p>";
            echo "</div>";
            echo "<p>comments... </p>";
            echo "<div class = 'comment_reply'>";
                echo "<span class = 'likecount'>Like</span>";
            echo "</div>";
        echo "</div>";
     }
}
}
?>

任何帮助我解决此问题的帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

追加所有那些echo变量并返回。

$html='';//before foreach

    $html = '<div class = 'per_comment'><a href = '#'><p class = 'usernames'>'. commenter_name.'</p></a><div class = 'commenter_details'><p>'. commenter_full_name.' </p></div><p>"comments... "</p><div class = 'comment_reply'><span class = 'likecount'>Like</span></div></div>';

echo $html//@ end

答案 1 :(得分:0)

你有没有错误 PHP:comments.php

echo "<a href = '#'><p class = 'usernames'>" commenter_name  "</p></a>";
echo "<p>"comments... "</p>";

表示文字:

echo "<a href = '#'><p class = 'usernames'>commenter_name</p></a>";
echo "<p>comments... </p>";

表示变量(但没有$ commenter_name的声明):

echo "<a href = '#'><p class = 'usernames'>".$commenter_name."</p></a>";