2017-09-23 48 views
0

我有一個簡單的播放計數器的網頁上的音頻文件。它適用於初始加載音頻文件,而不是在另一個jQuery的阿賈克斯也更新爲同一類等(播放文件)訪問動態創建的音頻對象播放

 jQuery(document).ready(function($) { 

      /*now events will only be bound to this instance*/ 
      $('body .sermonmp3').on('playing',function(){ 
       /* within event callbacks keep searches within the main container element*/      
       var fileID=$(this).attr('id'); 
       console.log(fileID); 
       var data = {file_id: fileID ,security:ChurchAdminAjax.security}; 

       jQuery.post(ChurchAdminAjax.ajaxurl, { 'action': 'ca_mp3_action','data': data }, 
        function(response){ 
         console.log(response); 
         $('body .plays').html(response); 

        } 
       ); 

      }); 

})不同的文件;

我在做什麼錯誤?

回答

0

通過將代碼放入函數並在每次創建新音頻元素時調用它來解決此問題。

 function bindEvents(){ 

      $('body .sermonmp3').on('playing',function(){ 

       var fileID=$(this).attr('id'); 
       console.log(fileID); 
       var data = {file_id: fileID ,security:ChurchAdminAjax.security}; 

       jQuery.post(ChurchAdminAjax.ajaxurl, { 'action': 'ca_mp3_action','data': data }, 
        function(response){ 
         console.log(response); 
         $('body .plays').html(response); 

        } 
       ); 

      }); 
     }