在列表顶部显示最后一条注释


display last comment at the top of the list

我有一个脚本,可以用AJAX将注释添加到DB中,然后在屏幕上打印,在前面的注释的末尾

我如何更改它,使新的评论(用户刚刚添加的评论)出现在评论的顶部?

$(function() {
    $(".addComment").click(function(){
        var element = $(this);
        var refID = element.attr("id");
        var userID = element.attr("userID");
        var main = $("#main:checked").val();
        var contentText = $("#contentText").val();
        var act = 'add';
        var info = 'refID=' + refID + '&userID=' + userID + '&main=' + main + '&contentText=' + contentText + '&act=' + act;
    //  alert(info);
        $("#loading").html('<img src="loader.gif" align="absmiddle">&nbsp;loading...');
         $.ajax({
            type: "POST",
            url: "ajax/comments.php",
            data: info,
            success: function(response){
            $("#loading").ajaxComplete(function()
            {
            }).slideUp();
             document.getElementById("contentText").value="";
             $("#comments").append(response);
        //  $('.formComment'+ownerID).fadeOut(200).hide();
        //  $('.formComment'+ownerID).fadeIn(200).show();
           }
         });
        return false;
    });
});

        <ul id="comments">
            <?PHP
                $query = mysql_query("SELECT * FROM `comments` WHERE (userID = '$_SESSION[userID]') ORDER BY date DESC"); 
                while($index = mysql_fetch_array($query))
                {
                  echo '<li id="comment_'.$index["id"].'">';
                      echo '<a href="#" class="del_button" id="del-'.$index["id"].'">';
                        echo '<img src="images/icon_del.gif" border="0" />';
                      echo '</a>';
                  echo $index["text"].'</li>';
                }                   
            ?>
        </ul>

PHP页面:

if(mysql_query("INSERT INTO comments (userID, pageName, refID, text, main, date) VALUES ('$_POST[userID]', 'yard', '$_POST[refID]', '$contentToSave', '$_POST[main]', '$dateLX') ") )
{
     //Record was successfully inserted, respond result back to index page
      $commentID = mysql_insert_id(); //Get ID of last inserted row from MySQL
      echo '<li id="comment_'.$commentID.'">';
        echo '<a href="#" class="del_button" id="del-'.$commentID.'">';
            echo '<img src="../images/deleteS.png" border="0" />';
        echo '</a>';
      echo $contentToSave.'</li>';
    //  mysql_close($connecDB); //close db connection
}

更改此行:

 $("#comments").append(response);

收件人:

 $("#comments").prepend(response);

http://jsfiddle.net/CXNF5/