Rails ajax执行很多次

时间:2012-02-28 01:30:44

标签: jquery ruby-on-rails ajax

好的,所以我已经构建了一个Rails应用程序,其中我有一些控制器从主页上的表单接收数据:

class UsersController < ApplicationController
  def some_method
    ...
    respond_to do |format|
      format.js { render "some_method_first" }
    end
  end
end

在some_method_first.js里面,我得到了:

$("#some_id").prepend("<section id='notice'>example!</section>");
$(function () {
  $("#notice").delay(500).fadeIn('slow', function () {
    $(this).delay(2500).fadeOut;
  });
});

棘手的部分是当我尝试在主页上输入内容并提交时,第一次没问题,但第二次,两个“例子!”出现,第三次,三个“例子!”部分出现......

我被疯了......有人能给我一些线索吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

看起来您每次都要添加新的section。如果你不刷新,旧的仍然会在那里,所以你只是继续前一个。

您需要检查#notice是否已经存在,或者更好,只需每次都删除它:

$("#notice").remove();
$("#some_id").prepend("<section id='notice'>example!</section>");
相关问题