duanmen2189 2012-07-14 17:16
浏览 63
已采纳

删除评论而不刷新页面

Would like to know how I can improve this code to enable no page refresh when I delete a comment. As its rather annoying if you delete a comment half way down the page and it reverts back to the top.

AJAX

    function delete_(pid){
    $.ajax({
       type: "POST",
       url: "../include/post.delete.php",
       data: "pid="+pid,
       success: function(){
       }
     });
    }

DELETEPOST
    echo'<div class="userinfo"><div id="divider">';

            if($streamitem_data['streamitem_type_id'] == 1||$streamitem_data['streamitem_type_id'] == 3){
                echo "";
                }else{
                echo "";
                }

                $poster_name = _user_core::getuser($streamitem_data['streamitem_creator']);
                $target_name = _user_core::getuser($streamitem_data['streamitem_target']);


            if($streamitem_data['streamitem_type_id'] == 1||$streamitem_data['streamitem_type_id'] == 3){
            if(!($streamitem_data['streamitem_type_id']==1)) {

                echo "<a href='/profile.php?username=".$poster_name['username']."'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped".$streamitem_data['streamitem_creator'].".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a>";

echo "<a href='/profile.php?username=".$target_name['username']."'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped".$streamitem_data['streamitem_target'].".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a>";
            }
                if(!($streamitem_data['streamitem_type_id']==3)) {
                echo "<a href='/profile.php?username=".$poster_name['username']."'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped".$streamitem_data['streamitem_target'].".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a>";

                }
                    $cont = stripslashes($streamitem_data['streamitem_content']);

                    if(!($streamitem_data['streamitem_type_id']==1)){
                    //$cont = htmlentities($cont);
                    $cont = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a class='user_link' href=\"\\0\">\\0</a>", $cont);

                    }


                    if($streamitem_data['streamitem_creator']==$streamitem_data['streamitem_target']){
                        echo "<a href='/profile.php?username=".$poster_name['id']."'>" . $poster_name['fullusersname']."</a><span class='subtleLink'> said </span>";
                    }else{
                        echo "<a href='profile.php?username=".$poster_name['username']."'>" .$poster_name['fullusersname']."</a>  
                        <span class='subtleLink'>wrote on </span>
                              <a href='profile.php?username=".$poster_name['username']."'>" .$target_name['fullusersname']." 's</a> stream";
                    }
                    echo "<br/><a href='#' class='subtleLink' style='font-weight:normal;'>".Agotime($streamitem_data['streamitem_timestamp'])."</a></div><hr><div style='padding-left:10px;padding-right:10px;'>";

                    if($streamitem_data['streamitem_type_id']==1){
                    $cont = nl2br($cont);
                    echo "<span class='subtleLink'>".$cont."</span>";
                    }else{

                        if($streamitem_data['streamitem_creator']==$streamitem_data['streamitem_target']){
                        $cont = nl2br($cont);
                            echo "<div>".$cont."</div>";
                        }else{
                        $cont = nl2br($cont);
                        echo "<div>'".$cont."</div>";

                        }

                    }


        echo "</div><div class='post_contextoptions'>";
    if($streamitem_data['streamitem_creator']==$_SESSION['id']){
        echo "<div class='stream_option' id='delete_option'><form style='display:inline;' id='delete_".$streamitem_data['streamitem_id']."'  action='include/post.delete.php' method='POST' target='ifr2'><input type='hidden' name='pid' value='".$streamitem_data['streamitem_id']."'><a style='cursor:pointer;position:relative;top:-70px;float:right;padding-right:5px;' onClick='document.forms[\"delete_".$streamitem_data['streamitem_id']."\"].submit();refreshstream();'>X</a></div>";

}

  • 写回答

1条回答 默认 最新

  • dsf487787 2012-07-14 17:36
    关注

    You need to call .remove() on the comment element you just deleted. You will need to figure out how to identify it. There is one way:

    function delete_(pid){
        $.ajax({
           type: "POST",
           url: "../include/post.delete.php",
           data: "pid="+pid,
           success: function(){
               // However you can identify the comment's div, and delete it by calling .remove()
               // This is if it has a unique id
               $("#comment-"+pid).remove();
           }
         });
    }
    

    Otherwise, if you used <a href="javascript:delete_(pid);"> you could add a parmeter like <a href="javascript:delete_(pid, this);">. This way you would get to the element via $(the_element_you_passed_to_the_function).parents("div.the-comment-div-parent").remove() for example.

    Otherwise you could implement this function right inside the $("a").click(function {}); if you are using this method. It's up to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line