2016-01-07 58 views
0

當點擊頁面鏈接時,我有網格,然後html從服務器端通過ajax調用返回並且新的網格html正在附加在div中。新的html內容有按鈕,其類名爲edit-userJQuery:動態頁面內容和附加按鈕點擊

所以我寫了這段代碼來附加jquery點擊按鈕,這是在頁面中動態添加.....但在我的情況下,它不工作。

$(function() { 
    $('#gridContent').on('click','.edit-user', function() { 
     var tr = $(this).parents('tr:first'); 
     $(tr).addClass('Editing'); 
     if ($(tr).find("td:nth-child(2)").hasClass('PadOn')) { 
      $(tr).find("td:nth-child(2)").removeClass("PadOn"); 
      $(tr).find("td:nth-child(3)").removeClass("PadOn"); 
      $(tr).find("td:nth-child(4)").removeClass("PadOn"); 
      $(tr).find("td:nth-child(5)").removeClass("PadOn"); 

     } 

     $(tr).find("td:nth-child(2)").addClass("PadOff"); 
     $(tr).find("td:nth-child(3)").addClass("PadOff"); 
     $(tr).find("td:nth-child(4)").addClass("PadOff"); 
     $(tr).find("td:nth-child(5)").addClass("PadOff"); 

     tr.find('.edit-mode, .display-mode').toggle(); 
     $(tr).find("input[id*='txtFirstName']").focus(); 
     return false; 
    }); 

我使用的是jquery文件版本v1.10.2。尋找幫助和建議,如我犯了哪個新的按鈕沒有附加jQuery按鈕點擊事件的錯誤。感謝

+0

'#gridContent'是否也是動態的? – Azim

+0

否...在#gridContent中添加ajax內容。 – Mou

回答

0

問題進行排序,此代碼

$(document).on('click', '.edit-user', function() { 
var tr = $(this).parents('tr:first'); 
     $(tr).addClass('Editing'); 
     if ($(tr).find("td:nth-child(2)").hasClass('PadOn')) { 
      $(tr).find("td:nth-child(2)").removeClass("PadOn"); 
      $(tr).find("td:nth-child(3)").removeClass("PadOn"); 
      $(tr).find("td:nth-child(4)").removeClass("PadOn"); 
      $(tr).find("td:nth-child(5)").removeClass("PadOn"); 

     } 

     $(tr).find("td:nth-child(2)").addClass("PadOff"); 
     $(tr).find("td:nth-child(3)").addClass("PadOff"); 
     $(tr).find("td:nth-child(4)").addClass("PadOff"); 
     $(tr).find("td:nth-child(5)").addClass("PadOff"); 

     tr.find('.edit-mode, .display-mode').toggle(); 
     $(tr).find("input[id*='txtFirstName']").focus(); 
     return false; 
    }); 

感謝

1

嘗試像以下。希望這會有所幫助。

$('body').on('click','.edit-user', function() { 
    //your code 
});