0

我在我的 Laravel 5.1 应用程序中使用 jScroll ( jscroll.com ) 进行无限滚动。我正在进一步使用一些 jquery,我希望在单击每个帖子的“Like”按钮时触发它。Jquery 在第一页的帖子上运行良好,即localhost/myproject/index,但它不会被 jScroll 从下一页(即localhost/myproject/index?page=2等)附加的帖子触发。

这是我显示帖子的代码:

    @foreach($posts as $post)
        <div class="panel panel-default">
            <div class="panel-body post">
                <h3>{{ $post->title }}</h3>
                <hr>
                <p>{{ $post->discripion }}</p>
            </div>
            <div class="panel-footer">
                <div class="btn-group">
                    <button type="button" data-id="{{$post->id}}" class="btn btn-default like-btn">Like</button>
                </div>
            </div>
    </div>
@endforeach

我想为每个帖子触发的简单 jquery 是:

<script type="text/javascript">
            $('button.like-btn').on('click',function(){
                var post_id = $(this).data('id');
                alert('Liked post with id = ' + post_id);

            });
        </script>
4

1 回答 1

1

这是因为 jquery 没有绑定到这些元素(它们最初不在 DOM 中)。而是将其绑定到文档,如下所示:

$(document).on("click", 'button.like-btn', function(event) { 
    alert("new link clicked!");
});

多看这里

于 2015-10-17T18:49:42.530 回答