簡體   English   中英

如何在先前從jquery附加的div內的元素上使用jquery onClick函數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM