2017-04-13 73 views
0

我有一個管理學院的示例web程序。在文檔加載時,會調用ajax來請求大學數據。將事件綁定到新添加的html內容AJAX Jquery

添加新大學時也會調用請求。當插入到mysql成功並完成時,再次調用大學數據的ajax調用。

$(document).on("click", ".ctrl-btn", function(e){ 
    if($(this).hasClass("college-edit-btn")) { 
    $(".college-edit-btn").on("click", editCollegeProcess); 
    } 
}); 
//The editCollegeProcess() simply alerts the id of the parent. 

該代碼的工作原理,但我不得不雙擊按鈕來觸發該功能的文件加載時間。在警報窗口處於活動狀態時單擊該按鈕時,它也會觸發該事件。

我希望我解釋清楚,請幫助。這太混亂了@@ ​​

+0

將一個事件處理程序放入另一個事件處理程序中幾乎是不對的。 – Barmar

+0

刪除裏面的''click''綁定並在外面像這樣使用'$(document).on(「click」,「.college-edit-btn」,editCollegeProcess);' –

+0

@MaheshSinghChouhan非常感謝。我真笨。 –

回答

0

刪除裏面"click"結合外使用它像這樣:

$(document).on("click", ".college-edit-btn", editCollegeProcess); 
0

你沒有約束.college-edit-btn處理程序,直到用戶首先點擊.ctrl-btn。您應該直接將事件委派放在主代碼中。

$(document).on("click", ".college-edit-btn", editCollegeProcess); 
+0

它看起來像我的評論順便說一句。 –

0

如果你必須有2個點擊事件,你可以把它們連:

$(document).on("click", ".ctrl-btn", function(e){ 
 
    e.stopPropagation();//if necessary 
 
}).on('click', 'college-edit-btn', editCollegeProcess{ 
 

 
});

假設有一個父母 - 孩子d關係。

+0

This works too !!!非常感謝。 :))))) –

相關問題