当我使用 Turbolinks 渲染部分 rails 后,Javascript 无法工作

标签 javascript ruby-on-rails ajax turbolinks-5

Partial 包含我的列表元素,它有赞成票反对按钮。它在第一次加载页面时工作正常,但是当我单击“加载更多”按钮时,我的“赞成票”和“反对票”按钮停止工作。但加载更多按钮仍然有效。

以下是我的代码,我在其中调用我的部分

<div class="container">
    <%= render :partial => 'show' ,:locals => {:@list1 => @list1}%>
</div>
<center><button id="load-more" class ="btn btn-outline-success" >load-more</button></center>

我的 JQuery 请求如下:

<script>
    var last_id = 3
    var id = <%= @post.id%>
    $(document).on("turbolinks:load",function(){

        $('button#load-more').click(function (e){
            e.preventDefault();

            last_id = last_id + 2;
            console.log(last_id)
            $.ajax({
                type: "GET",
                url: $(this).attr('href'),
                data: {
                    value: last_id
                },
                dataType: "script",
            });  


        });

        $('.vote').click(function(e){

            var k = $(this).parent().attr("id")
            if(k == "upvote"){
                var ajax_path = <%= @post.id%>+"/lists/"+this.id+"/upvote"

            }
            if(k == "downvote"){
                var ajax_path = <%= @post.id%>+"/lists/"+this.id+"/downvote"
            }

            $.ajax({
                url: ajax_path,  
                method:"POST",
                data: { },
                dataType: 'script'

            });

        })

    });


    </script>

我的 Rails Controller :

def show
      @post = Post.find(params[:id])
      if params[:value]
        @list1 = @post.lists.order({:upvotes => :desc},:downvotes).limit(params[:value])
      else
        @list1 = @post.lists.order({:upvotes => :desc},:downvotes).limit(3)
      end

      @comments = @post.comments.order("created_at DESC")

      respond_to do |format|
        format.html
        format.js
      end


    end

我的show.js.erb文件如下:

$('.container').html('<%= escape_javascript(render :partial => 'show' ,:locals => {:@list1 => @list1}) %>')

提前谢谢您。

最佳答案

尝试下面的脚本

<script>
    var last_id = 3
    var id = <%= @post.id%>
    $(document).on("turbolinks:load",function(){

        $(document).on("click" , "button#load-more", function (e){
            e.preventDefault();

            last_id = last_id + 2;
            console.log(last_id)
            $.ajax({
                type: "GET",
                url: $(this).attr('href'),
                data: {
                    value: last_id
                },
                dataType: "script",
            });  


        });
        $(document).on("click" , ".vote", function(e){

            var k = $(this).parent().attr("id")
            if(k == "upvote"){
                var ajax_path = <%= @post.id%>+"/lists/"+this.id+"/upvote"

            }
            if(k == "downvote"){
                var ajax_path = <%= @post.id%>+"/lists/"+this.id+"/downvote"
            }

            $.ajax({
                url: ajax_path,  
                method:"POST",
                data: { },
                dataType: 'script'

            });

        })

    });


    </script>

如果要渲染动态部分,则需要绑定(bind)实时事件。让我知道它是否有效

关于当我使用 Turbolinks 渲染部分 rails 后,Javascript 无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49187144/

相关文章:

javascript - 如何检查数组中是否有一个或多个项目以及为什么此方法不起作用?

ruby-on-rails - 如何测试回形针 URL?

javascript - 如何用jquery获取最后一个元素的id值?

javascript - 无需刷新页面即可显示本地存储变量

javascript - 使用 javascript 查找 div 标签显示属性为 none 或 block 或 undefined

javascript - Highcharts - 图表回流功能问题

ruby-on-rails - 在 Rails Controller 测试中,有没有办法模拟特定的远程 IP?

ruby-on-rails - Rails 设计自定义注册和登录在同一页面问题

javascript - jQuery - document.ready 在使用 AJAX 加载内容时不触发

javascript - 如何在 javascript 中获取这个选择器?