简体   繁体   中英

when scroll down, data is loaded by ajax but click trigger is not working jquery

i face a problem of jquery click function when i scroll down in list.

  1. in list, data is loaded by ajax request and when i scroll down then click function (trigger) is not working.
  2. when i not scroll down, ajax data is not loaded then click function is working.

i'm confuse why this happened. i used following triggers below but not success.

  1. on()
  2. click()
  3. bind()
  4. load()
  5. delegate()

i'm sending you code. this is code below. Please help me to sort out.

$(window).ready(function(){ $(".like").on("click", function(){

        var id = $(this).attr('data');
        // alert(id);

        $.ajax({
            url: "/stores/addLike",
            type: 'GET',
            data: {
                id: id
            },
            error: function (data) {
               console.log(data);
            },
            success: function (data) {
                console.log(data);
                if(data == "liked"){
                    alert('Sorry, you have already liked this beat.');
                }else if(data == "notlogin"){
                    alert('Please login first to like a beat.');
                    window.location = "https://demo.amplifihub.com/login";
                }else{
                $(".likes"+id).text(data);
                }
            }
        });
    });
});

instead of $(".like").on("click", function(){ }); use $(document).on("click", ".like", function(){}); or use any static parent dom element to preserve the DOM event. I believe you are generating .like class element on scroll ajax call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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