2012-07-14 175 views
0

想知道如何改進此代碼以便在刪除評論時不刷新頁面。因爲如果你在頁面下半部刪除了一條評論,並且它回到頂端,那麼它就相當麻煩。刪除評論而不刷新頁面

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

有成功處理程序刪除評論的DOM節點? – 2012-07-14 17:17:55

+0

馬克,我該怎麼做? – dave 2012-07-14 17:21:24

回答

2

你需要調用.remove()剛刪除的評論元素。你需要弄清楚如何識別它。還有一種方法:

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

否則,如果您使用的<a href="javascript:delete_(pid);">您可以添加一個parmeter像<a href="javascript:delete_(pid, this);">。例如,通過這種方式,您將通過$(the_element_you_passed_to_the_function).parents("div.the-comment-div-parent").remove()訪問該元素。

否則,如果您使用此方法,則可以在$("a").click(function {});內部實現此功能。隨你便。

+0

評論元素是評論在裏面的div嗎? – dave 2012-07-14 18:21:13

+0

我更新了我的代碼,我的流發佈是因爲我不知道我需要調用什麼。如果有人可以幫忙/ – dave 2012-07-14 18:36:59

+0

是的,包含所有你想隱藏的div應該被刪除。如果你可以發佈一個HTML輸出的例子,你的頁面會產生好的結果,這樣我們就可以知道你的HTML結構是什麼。真的不清楚你發佈的代碼片段是什麼。我*相信*你應該刪除class'userinfo'(與'div.userinfo'匹配)的div,但這個名字似乎並不相關,所以我不確定。 – jadkik94 2012-07-15 19:11:31