dtz33344 2012-07-14 23:53
浏览 44
已采纳

使用Ajax删除评论

What is the best possible way/results using a link a div and ajax to pass data without a page refresh. I'm wanting my comments and div to fade out when clicked and don't want the page to refresh. So it stays exactly where I delete the comment.

I've tried this, but its not working

   <script>
function delete_(pid){
$.ajax({
   type: "POST",
   url: "include/post.delete.php",
   data: "pid="+pid,
   success: function(){
   $("#comment-"+pid).remove();
   }
 });
}
</script>
<? if($streamitem_data['streamitem_creator']==$_SESSION['id']){
echo "<div style='cursor:pointer;' onclick=\"delete_('".$streamitem_data['streamitem_id']."');\">Delete comment</div>";

But it's not giving me the above results I need. Maybe I'm doing something wrong, but I need help if possible.

  • 写回答

1条回答 默认 最新

  • dpgkg42484 2012-07-15 00:35
    关注

    Don't use a form with a submit action to delete that comment. When submitting a form, the page is automatically reloaded. Instead use a simple element with an onclick handler to trigger the AJAX request:

    <div onclick="delete_('anyID');">Delete comment</div>
    

    EDIT: Example:

    <head>
    <script src="jquery.min.js"></script>
    <script type="text/javascript">
    function delete_(pid){
    $.ajax({
       type: "POST",
       url: "include/post.delete.php",
       data: "pid="+pid,
       success: function(){
       $("#comment-"+pid).remove();
       }
     });
    }
    </script>
    </head>
    
    <body>
    <div id="comment-123">This is a comment.</div>
    <div onclick="delete_('123');">Delete comment</div>
    </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?