简体   繁体   English

ajax调用后Javascript不起作用

[英]Javascript does not work after ajax call

I'm trying to do an Ajax call to get more data on click. 我正在尝试进行Ajax调用以获取更多点击数据。 It hass worked at the first click, but after that I can't click the button anymore, also the data I received not apply any javascript (like bootstrap popover, lazyload img). 第一次单击它就可以了,但是之后我再也不能单击该按钮,所以我收到的数据也不会应用任何JavaScript(例如bootstrap popover,lazyload img)。 I have placed the JS in the bottom (before the </body> tag) and I thought that's the problem so I'm trying to put js in the <head> tag but that still doesn't fix the problem. 我将JS放在底部(在</body>标记之前),我认为这是问题所在,因此我试图将js放在<head>标记中,但这仍然不能解决问题。

I've found solution on stackoverflow like 我已经找到了关于stackoverflow的解决方案,例如

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

but that doesn't fix it. 但这并不能解决。 This is my js: 这是我的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);
        }
    }); 
});

sorry if my English is bad because I'm Vietnam. 如果我的英语不好,很抱歉,因为我是越南人。

Just make sure where the problem is checking if the button event and the ajax request work using console.log() . 只需使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM