2016-11-07 100 views
0

我正在嘗試做一個Ajax調用來獲取有關點擊的更多數據。它在第一次點擊工作,但在此之後,我不能再點擊按鈕,我收到的數據也不會應用任何JavaScript(如bootstrap popover,lazyload img)。我把JS放在底部(在</body>標籤前面),我認爲這是問題,所以我試圖將js放在<head>標籤中,但仍然無法解決問題。Ajax調用後Javascript不工作

我發現在計算器的解決方案就像

$(document).on('click','.show_more',function(){ 
    // function 
}); 

但仍不能解決問題。這是我的js:

$(document).on('click','.show_more',function(){ 
    var ID = $(this).attr('id'); 
    $('.show_more').hide(); 
    $('.loding').show(); 
    $.ajax({ 
     type:'POST', 
     url:'load.php', 
     data:'id='+ID, 
     success:function(html){ 
      $('#show_more_main'+ID).remove(); 
      $('.item-list').append(html); 
     } 
    }); 
}); 

對不起,如果我的英語不好,因爲我是越南。

+1

你是隱藏的按鈕,而不是把它回來:) – markoffden

+0

@markoffden不,我把它放回load.php但我不能再單擊它。按鈕只是第一次工作。 –

+0

非常感謝。我已經解決了這個問題。 –

回答

0
clickEvent()(); 
function clickEvent() { 
$(document).on('click','.show_more',function(){ 
var ID = $(this).attr('id'); 
$('.show_more').hide(); 
$('.loding').show(); 
$.ajax({ 
    type:'POST', 
    url:'load.php', 
    data:'id='+ID, 
    success:function(html){ 
     $('#show_more_main'+ID).remove(); 
     $('.item-list').append(html); 
     clickEvent(); 
    } 
    }); 
}); 
} 
+0

非常感謝你,popover和lazyload是工作,但按鈕不能再次點擊。 –

+0

做遞歸函數,我會編輯我的答案 –

+0

謝謝你,我解決了這個問題。非常感謝你。 –

1

只要確定問題出在哪裏檢查按鈕事件和ajax請求是否使用console.log()工作。

$(document).on('click','.show_more',function(){ 
      console.log("my button click works"); 
      var ID = $(this).attr('id'); 
      $('.show_more').hide(); 
      $('.loding').show(); 
      $.ajax({ 
       type:'POST', 
       url:'load.php', 
       data:'id='+ID, 
       success:function(html){ 
        console.log("my ajax request was successful") 
        $('#show_more_main'+ID).remove(); 
        $('.item-list').append(html); 
       } 
      }); 
     }); 
+0

這是工作,謝謝你 –