weixin_33743248 2017-06-12 15:04 采纳率: 0%
浏览 66

如何重启JS功能?

I have a script for "like" button. When I am clicking to button, AJAX sending data to videolike.php

  1. First I am getting videos from database using PHP with limit 10

  2. After this is loading this script...

    <script type="text/javascript">
         $(".vidlike").click(function() {
         var form = $(this).closest(".vidlikform");
    
         $.ajax({
              type: 'post',
              url: "functions/videolike.php",
              data: form.serialize(), // serializes the form's elements.
              success: function(data)
              {
                  alert(data); // show response from the php script.
              } 
         });
         return false; // avoid to execute the actual submit of the form.
    });
    </script>
    

Now I have 10 videos in my HOME page, The script is working fine. Sending data to videolike.php without redirecting...

The problem is this script is working only for first 10 videos, 'its not working for next videos i got from database, redirecting me to videolike.php...

This is the script I am using for get more data:

<img class="load-more" id="<?php echo @$var['video_id']; ?>" src="img/loader.gif"/>

<script type="text/javascript">
    //Ajax more data load Home page
    $(document).ready(function(){
        $(window).scroll(function(){
            var lastID = $('.load-more').attr('id');
            if ($(window).scrollTop() == $(document).height() - $(window).height() && lastID != 0){
                $.ajax({
                    type:'POST',
                    url:'functions/getData.php',
                    data:'id='+lastID,
                    beforeSend:function(html){
                        $('.load-more').show();
                    },
                    success:function(html){
                        $('.load-more').remove();
                        $('#main').append(html);
                    }
                });
            }
        });
    });

    </script>
  • 写回答

1条回答 默认 最新

  • George_Fal 2017-06-12 18:12
    关注

    You will want to use a delegated event handler.

    This will attach a single listener, and compares the clicked target against the selector, in this case .vidlike.

    This will work even if you modify the DOM by adding more results.

    $('body').on("click", ".vidlike", function() {
         var form = $(this).closest(".vidlikform");
    
         $.ajax({
              type: 'post',
              url: "functions/videolike.php",
              data: form.serialize(), // serializes the form's elements.
              success: function(data)
              {
                  alert(data); // show response from the php script.
              } 
         });
         return false; // avoid to execute the actual submit of the form.
    });
    

    See the docs for .on() for more info.

    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染