[英]how to use jquery onClick function on element inside a div which were previously appended from jquery
我已經在類.display_noti
的div中添加了一些元素,例如:
$(document).ready(function(){
setTimeout(done,200);
});
function done() {
setTimeout(updates, 200); // Call updates in 200ms
}
function updates(){
$.getJSON("notification.php",function(data){
// you can do checking here
if ( data.result && data.result.length > 0 ) {
$(".display_noti").empty(); // Clear out the div
$.each(data.result,function(){
$(".display_noti").append("<div class='palnotific'>You got a pal request from <strong>"+this['from_user']+"</strong><br><span class='date'>"+this['notification_date']+"<form method='post'><input type='hidden' value='"+this['pals_id']+"'></form></div>");
});
done();
}
else {
$(".display_noti").append("<div class='palnotific' style='background-color:white;'>You have no notification to view.</div>");
}
});
}
我有fetched
由數據database
,並轉換它json encode
格式,然后我用上面的jquery函數來獲得,驗證和添加內部那些信息div
與類.display_noti
。
大家可以看到我正在為通知系統執行所有操作,因此每次此代碼檢查數據庫中是否有新的更新時。如果數據庫中有某些更改,則.display_noti
div中的每200 milliseconds
元素中具有.palnotific
類的div將自動更新,而無需重新加載/刷新任何頁面。
現在我的問題是,每200 milliseconds
這些appended
div消失並再次出現。如果有任何新更新它將添加到我的.display_noti
是很好的,但是如果我想添加一些jquery event handler
函數可能就不那么好了像onclick
一樣。我的意思是我想在該附加的div
上添加一些onclick
函數。我想采用該form
輸入值,並通過ajax
將其發送到該過程的php
文件中,並對數據庫進行一些更改。要在這種情況下做到這一點?
(在頁面中,事情不會消失。我所說的是每200 milliseconds
出現一次並消失重復的源代碼)
$('document').on('click','.palnotific',function(){
input_val = $(this).find(':input').val();
//continue ajax here
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.