2015-11-03 46 views
0

所附我已經所附一些元件一個div內側.display_noti類像一個div內: -如何使用jquery的onClick功能元件在其上先前從jquery的

$(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>"); 
    } 
    }); 
} 

我已經從databasefetched數據和將它轉換在json encode格式,然後我使用上面的jquery函數來獲取,驗證並添加那些div.display_noti中的信息。

正如你們可以看到我做的一切都是爲了我的通知系統,所以,每到這個代碼檢查可用與否在新的更新時間我database.If有這麼.display_noti DIV裏面的一些變化,數據庫,然後在每一個200 milliseconds元素這意味着與類.palnotific div將自動更新沒有任何頁面刷新/刷新。

現在我的問題是,每一個200 milliseconds那些appended格獲得消失,然後再again.It的好,如果有任何新的更新會增加對我的.display_noti,但如果我想添加一些jQuery的可能是並不好event handler功能如onclick。我的意思是我想添加一些onclick功能,在附加的div。我想採取form輸入值,並通過ajax發送到php文件的過程,並對數據庫進行一些更改。什麼是正確的在這種情況下如何做到這一點?人們需要幫助。

(在頁面的東西不vanish.I說的是,每一個200 milliseconds的事情出現,消失,重複的源代碼)

+0

是pals_id是自動增量在你的數據庫? –

+0

爲什麼@VigneswaranS兄弟? –

+0

我會給出一個想法。只是嘗試它。如果你的pals_id是auto_increment,那麼我們可以得到最大的價值。它被附加在你的div.take該值,並作爲變量(var pal)。下一次更新檢查var pal <= this [Pals_id] .if true append else leave –

回答

-1
$('document').on('click','.palnotific',function(){ 
    input_val = $(this).find(':input').val(); 

    //continue ajax here 
}); 
+0

請考慮編輯您的帖子,以添加更多關於您的代碼的解釋以及爲什麼它可以解決問題。一個主要包含代碼的答案(即使它正在工作)通常不會幫助OP瞭解他們的問題。 – SuperBiasedMan

相關問題