2013-12-12 55 views
0

好吧,我有一個編輯按鈕,當我按下它,它改變爲「完成」按鈕。jQuery的不能識別類變化

這一切都通過jQuery的完成。

 $(".icon-pencil").click(function() { 
      var pencil = $(this); 
      var row = $(this).parent('td').parent('tr'); 
      row.find('td').not(":nth-last-child(2)").not(":last-child").each(function() { 
       $(this).html("hi"); 
      }); 

      pencil.attr('class', 'icon-ok-sign'); 
     }); 

     // save item 
     $(".icon-ok-sign").click(function() { 
      alert("hey"); 
     }); 

當我按下一個「編輯」(「.icon-pencil」)按鈕,它的類更改爲.icon-ok-sign(我可以在Chrome控制檯中看到),

但是當我點擊它,顯示沒有警告。

當我創建它<span class="icon-ok-sign">press</span>和壓機,警報顯示。

如何解決?

回答

1

使用$(document).on("click", ".icon-ok-sign", function() {...

+0

不知道這個作品!這是答案。 – schlingel

+0

現在它調用兩次。 。 – Billie

+0

我們需要更多信息來確定爲什麼要調用兩次。 – Stian

0

使用try以下腳本:

$(document).on('click','.icon-ok-sign',function(){ 
alert("hey"); 
}); 
0

那是因爲你不能註冊點擊的事件對未來的元素,你必須做這樣的:

此方法提供了將委派的事件處理程序附加到文檔元素 的方法當內容被動態添加到頁面時,它簡化了事件處理程序 的使用。

+0

現在它調用兩次。 。 – Billie

+0

你必須刪除自己的點擊通話:$( 「圖標-OK-跡象。 」)點擊(函數(){ 警報(「 哎」); }); – Mark

+0

你剛纔複製了斯坦森的答案! – schlingel

0

試試這個:

$(".icon-pencil").click(function() { 
      var pencil = $(this); 
      var row = $(this).parent('td').parent('tr'); 
      row.find('td').not(":nth-last-child(2)").not(":last-child").each(function()   { 
       $(this).html("hi"); 
      }); 

      pencil.removeAttr('class').addClass('icon-ok-sign'); 
     }); 

     // save item 
     $(".icon-ok-sign").click(function() { 
      alert("hey"); 
     });