2012-05-11 22 views
2

我有一種情況,當用戶單擊#postComment鏈接時,我必須提交註釋。Jquery使用這個關鍵字選擇一個類

現在使用此代碼,第一次評論提交成功,但不是第二次。我認爲這是因爲jquery不合適,它在這裏越來越困惑$('。comment a')。

現在我想知道如何使用「this」關鍵字或任何其他可能的解決方案訪問這些類型的變量。

每次提交評論時,newCommentBox變量都會附加到commentWrapper以生成新的評論框。

的Jquery:

$('.comment a').click(function(){ 


comment="<pre>"+$('textarea').val()+"</pre>"; 
newcommentBox="<div class='CommentBox'>" 
     +"<div class='dp'><img src='../Images/defaultPic.jpg' width='50' height='50' /></div>" 
     +"<div class='name'>Muazzam Ali</div>" 
     +"<div class='comment'>" 
     +"<textarea cols='70' rows='10'></textarea><br />" 
      +"<a href='javascript:void(0)' id='postComment'><img src='../Images/commentbutton.png' height='30' width='90' /></a>" 
     +"</div>" 
    +"</div>"; 

$(this).prev().html(""); 
$(this).hide(); 

$(this).parent().html(comment); 
$('#CommentWrapper').append(newcommentBox); 

}); 

HTML:

<div id="CommentWrapper"> 
      <div id="CommentHeading">Comments:</div> 
    <div class="CommentBox"> 
     <div class="dp"><img src="../Images/defaultPic.jpg" width="50" height="50" /></div> 
       <div class="name">Muazzam Ali</div> 
       <div class="comment">Comment</div> 
       </div> 

    <div class="CommentBox"> 
     <div class="dp"><img src="../Images/defaultPic.jpg" width="50" height="50" /></div> 
     <div class="name">Muazzam Ali</div> 
     <div class="comment"> 
     <textarea cols="70" rows="10"></textarea><br /> 
      <a href="javascript:void(0)" id="postComment"><img src="../Images/commentbutton.png" height="30" width="90" /></a> 
     </div> 
    </div> 
</div> 

回答

1

它不加註釋的第二次,因爲你還沒有加入click,甚至處理到代表提交評論新a元素按鈕。您實際上必須再次將事件處理程序添加到此新創建的元素。或者,使用jQuery的委託功能,使用on方法,將事件處理程序始終添加到您的a元素中。

不過,就我個人而言,這是我所要做的。下面是更新後的JavaScript:

$('.comment a').click(function _this(){ 

    comment = "<pre>"+$('textarea').val()+"</pre>"; 
    newcommentBox = "<div class='CommentBox'>" 
     +"<div class='dp'><img src='../Images/defaultPic.jpg' width='50' height='50' /></div>" 
      +"<div class='name'>Muazzam Ali</div>" 
      +"<div class='comment'>" 
       +"<textarea cols='70' rows='10'></textarea><br />" 
       +"<a href='javascript:void(0)' id='postComment'><img src='../Images/commentbutton.png' height='30' width='90' /></a>" 
      +"</div>" 
     +"</div>"; 

    $(this).prev().html(""); 
    $(this).hide(); 

    $(this).parent().html(comment); 
    $('#CommentWrapper').append(newcommentBox); 
    $('#CommentWrapper').find(".comment a").click(_this); 

}); 

我在做什麼這是命名的函數表達式你傳遞給$(".comment a").click()名爲_this。這個名字我已經給出了這個函數的功能只有這個函數本身。任何時候發表評論時,它都將其自身附加爲下一個a元素的單擊事件處理程序。它可以讓你避免使用事件代理,其中可以招致性能損失。 (如果你關心之類的事情。)

+0

所以,我怎麼能做到呢.. – Mj1992

+0

@ Mj1992我給你的代碼的解釋。 –

+0

thnx它解決了問題 – Mj1992

0

嘗試使用代理(假設的jQuery 1.7)

$(document.body).on("click", ".comment a", function(e) { 
// handle event 
}); 
+0

如果不是1.7,它是'delegate' FYI :) – mattytommo

0

我相信問題可以是您要附加回調後追加新的按鈕到點擊事件。

嘗試replacting的$(".comment a").click(function()...)與到以下幾點:

$(".comment a").live("click", function()...);,它也將連接到新的