Javascript在ajax调用后不起作用

Javascript does not work after ajax call

本文关键字:不起作用 调用 ajax Javascript      更新时间:2023-09-26

我试图做一个Ajax调用,以获得更多的数据点击。它在第一次点击工作,但之后我不能再点击按钮,也我收到的数据不应用任何javascript(如bootstrap弹出窗口,懒加载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);
        }
    }); 
});

抱歉我的英语不好,因为我是越南人。

请确保问题在哪里检查按钮事件和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);
                }
            }); 
        });
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();
    }
  }); 
});
}