2015-08-20 36 views
0

我在牆上有類似Facebook的東西。每篇文章都有一些評論如下。 我想要做的是編輯評論選項。 它用textarea表單替換當前評論,接受後顯示新評論。 該按鈕不能很好地工作。它應該是動態的(在不刷新網站的情況下工作)。但是我有一些困難。編輯鏈接在其他評論下打開textarea,或者在textarea框中顯示第一個評論(新的應該會出現......)我現在要做的是將.data()固定到「this」按鈕,然後它將包含新的評論文本,所以我將能夠將其動態讀取到開放的textarea。我並不完全確定自己在做什麼,而且我不清楚使用.this,但我希望你能幫我解決問題。該警報顯示「未知數據」。有沒有更好的解決方案,不需要js的master技能?把.data放到這個物體上

<a href="#!" class="editButton" onClick="editComment({{$comment->h_id}}, `{{$comment->f_text}}`)"> 

<script> 
function editComment(id, text){ 

    var thisId = "#" + id; 
    $(thisId).replaceWith("<form class='newCommentForm'>\n\ 
          <textarea id='newComment'>" + text + "</textarea>\n\ 
          <input type='button' id='submit' value='save'>\n\ 
          </form>"); 

      $('#submit').click(function() { 

     var newComment = document.getElementById('newComment').value;    
       $.ajax({ 
        type: 'get', 
        url: '/editcomment', 
        data: {newComment: newComment, 
           id: id}, 

        success:function(){ 
          $(".newCommentForm").replaceWith("<span id='" + id + "' class='limitedText shorter' style>" + newComment + "</span>"); 
          $(".editButton", this).data("PinComment", "newComment"); 
          alert($(".editButton", this).data("PinComment")); 
         }, 
        error:function(){ 
          alert('error'); 
         } 
       });      
      }); 


} 
</script> 

回答

0

在您的ajax調用中,this不再引用DOM元素。

編輯:

首先把你的評論對象變量

$thisComment = $(thisId); 

然後改變:

$(".editButton", this) 

到:

$(".editButton", $thisComment) 
+0

那麼如何I R那麼請參閱特定評論? – UberProCoder

+0

@Mateusz剛剛編輯我的答案。 – yazfield