Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery Ajaxed分页在rails 4上未按预期工作_Jquery_Ajax_Ruby On Rails 4 - Fatal编程技术网

Jquery Ajaxed分页在rails 4上未按预期工作

Jquery Ajaxed分页在rails 4上未按预期工作,jquery,ajax,ruby-on-rails-4,Jquery,Ajax,Ruby On Rails 4,在rails4应用程序上,我有一个小脚本来处理ajaxified分页。下面的代码通过index.js.erb获取HTML块,并假定作为脚本执行,并在新加载的分页链接上绑定单击事件: // pagination.js $(function() { $(".pagination a").on("click", $(".pagination a"), function() { $(".pagination").html("Loading..."); $.getScript(this

在rails4应用程序上,我有一个小脚本来处理ajaxified分页。下面的代码通过
index.js.erb
获取HTML块,并假定作为脚本执行,并在新加载的分页链接上绑定
单击
事件:

// pagination.js
$(function() {
  $(".pagination a").on("click", $(".pagination a"), function() {
    $(".pagination").html("Loading...");
    $.getScript(this.href);
    return false;
  });
});
然后
index.js.erb

// index.js.erb
$("#books").html("<%= escape_javascript(render("books")) %>");
这似乎不对,因为1。这是不干燥和2。我期望('click,function(){…})上出现
只是工作;像
.live(click,function(){…})
方法在较旧版本的jQuery中也有类似的功能


我在这里遗漏了什么吗?

之所以发生这种情况,是因为单击事件没有绑定到页面上动态创建的内容,它只绑定到页面加载时出现的元素(在您的例子中是
.pagination a
),因此它只工作一次。 要做到这一点,您必须绑定click事件,如下所示

$(document.body).on("click", ".pagination a", function(){
  //your code goes here..
});
更多信息:

事件处理程序仅绑定到当前选定的元素;他们 必须在代码调用.on()时存在。 为了确保元素存在并且可以选择,请将脚本放在HTML标记中的元素之后,或者在文档就绪处理程序中执行事件绑定。或者,使用委派事件附加事件处理程序

委托事件的优点是可以处理来自 以后添加到文档中的子元素

你的意思是说
.on(“click”、$(“.pagination a”),function(){…}
应该是
。on(“click”、“.pagination a”,function(){…}
?不是,它的
$(document.body)
,它的发生是因为你没有使用委托事件。
$(document.body).on("click", ".pagination a", function(){
  //your code goes here..
});