[英]Javascript does not work after ajax call
我正在嘗試進行Ajax調用以獲取更多點擊數據。 第一次單擊它就可以了,但是之后我再也不能單擊該按鈕,所以我收到的數據也不會應用任何JavaScript(例如bootstrap popover,lazyload img)。 我將JS放在底部(在</body>
標記之前),我認為這是問題所在,因此我試圖將js放在<head>
標記中,但這仍然不能解決問題。
我已經找到了關於stackoverflow的解決方案,例如
$(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);
}
});
});
如果我的英語不好,很抱歉,因為我是越南人。
只需使用console.log()確保問題在哪里檢查按鈕事件和ajax請求是否有效。
$(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);
}
});
});
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();
}
});
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.