Rails 3.1 远程按钮不引人注目的 Javascript 事件未捕获(JQuery)

发布于 2024-12-06 14:23:19 字数 2388 浏览 0 评论 0原文

我想开始使用 Ajax 事件 ajax:success、ajax:failure、ajax:complete 和 ajax:beforeSend 作为在帖子中推荐的不引人注目的 Javascript:

但由于某种原因它对我不起作用。我错过了一些东西(一些小东西),因为我无法让事件触发我的 Javascript。我希望有人能够发现我的代码中的“明显”错误/遗漏。

关于我的环境的一些详细信息:

  • Rails 3.1
  • jQuery (jquery-rails gem)
  • therubyracer for server side jsprocessing(这对我的示例来说并不重要)

为了尝试找出我所缺少的内容,我创建了一个简单的测试应用程序,其中有一个单个远程按钮。单击按钮时,我希望触发警报框。这个应用程序的代码可以在github上看到:

http://github.com/jpmcgrath/rbtest

我已将应用程序部署到heroku:

http://rbtest.heroku.com/projects/

如果你看看在应用程序中,您可以单击该按钮,该按钮成功创建了一个新项目(查看它手动刷新),但 ajax:success 事件似乎没有发生?

代码的内容如下:

在projects_controller.rb中

def remote_test
  @project = Project.new(:name => "remote test")

  respond_to do |format|
    if @project.save
      puts "project saved!\n\n\n"
      format.html { redirect_to @project, notice: 'Project was successfully created.' }
      format.json { render json: @project, status: :created, location: @project }
    else
      format.html { render action: "new" }
      format.json { render json: @project.errors, status: :unprocessable_entity }
    end
  end
end

在application.js中

jQuery(document).ready(function() {

  jQuery(".remote_button").click(function() {
    alert('Handler for .click() called.');
  })
  .bind("ajax:complete", function() {
    alert('complete!');
  })
  .bind("ajax:beforeSend", function () {
    alert('loading!');
  })
  .bind("ajax:error", function (xhr, status, error) {
    alert('failure!');
  })
  .bind('ajax:success', function(event, data, status, xhr) {
    alert('success!');
  });
});

在视图projects/index.html.erb中

<%= button_to "remote test", remote_test_path, :remote => true, :class => 'remote_button'  %>

如果有人可以指出我所缺少的内容(我怀疑它与响应类型有关),那么它会不胜感激。

I want to start using the Ajax events ajax:success, ajax:failure, ajax:complete and ajax:beforeSend as recommended for unobtrusive Javascript in posts like:

But for some reason it's not working for me. I am missing something (something small) as I cannot get the events to trigger my Javascript. I am hoping someone could spot the "obvious" mistake/omission in my code.

Some details on my environment:

  • Rails 3.1
  • jQuery (jquery-rails gem)
  • therubyracer for server side js processing (not that it should matter for my example)

To try and work out what I am missing I have created a simple test application which has a single remote button to. When the button is clicked, I want alert boxes to fire. The code of this app can be seen on github here:

http://github.com/jpmcgrath/rbtest

I have deployed the application to heroku here:

http://rbtest.heroku.com/projects/

If you look at the app, you can click the button and the button successfully creates a new project (to see it manually refresh), but ajax:success event doesn't seem to happen?

The guts of the code is as follows:

In projects_controller.rb

def remote_test
  @project = Project.new(:name => "remote test")

  respond_to do |format|
    if @project.save
      puts "project saved!\n\n\n"
      format.html { redirect_to @project, notice: 'Project was successfully created.' }
      format.json { render json: @project, status: :created, location: @project }
    else
      format.html { render action: "new" }
      format.json { render json: @project.errors, status: :unprocessable_entity }
    end
  end
end

In application.js

jQuery(document).ready(function() {

  jQuery(".remote_button").click(function() {
    alert('Handler for .click() called.');
  })
  .bind("ajax:complete", function() {
    alert('complete!');
  })
  .bind("ajax:beforeSend", function () {
    alert('loading!');
  })
  .bind("ajax:error", function (xhr, status, error) {
    alert('failure!');
  })
  .bind('ajax:success', function(event, data, status, xhr) {
    alert('success!');
  });
});

In the view projects/index.html.erb

<%= button_to "remote test", remote_test_path, :remote => true, :class => 'remote_button'  %>

If anyone could point out what I am missing (I suspect it has something to do with response type) it would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸩远一方 2024-12-13 14:23:19

您的事件处理程序未触发的原因是您的选择器位于错误的元素上。 Button_to 创建一个带有单个输入标签的表单,并且该输入标签具有您选择的类,但 ajax 事件是在表单上触发的,而不是输入标签。

试试这个,

jQuery(document).ready(function() {

  jQuery(".remote_button").click(function() {
    alert('Handler for .click() called.');
  });

  jQuery("form.button_to").bind("ajax:complete", function() {
    alert('complete!');
  })
  .bind("ajax:beforeSend", function () {
    alert('loading!');
  })
  .bind("ajax:error", function (xhr, status, error) {
    alert('failure!');
  })
  .bind('ajax:success', function(event, data, status, xhr) {
    alert('success!');
  });
});

我在我的 firebug 控制台中做了等效的操作,当回调位于表单上时,它触发了 ajax:success 处理程序。

要更改表单上的类,请使用 :form_class 选项将其从 button_to 更改为其他内容

<%= button_to "remote test", remote_test_path, :remote => true, :class => 'remote_button', :form_class => 'remote_button_form' %>

添加 ajax 回调

jQuery(".remote_button_form").bind ...

然后使用http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

The reason your event handler is not firing is because your selector is on the wrong element. button_to creates a form with a single input tag, and it's that input tag that has the class your selecting on, but the ajax events are triggered on the form, not the input tag.

Try this

jQuery(document).ready(function() {

  jQuery(".remote_button").click(function() {
    alert('Handler for .click() called.');
  });

  jQuery("form.button_to").bind("ajax:complete", function() {
    alert('complete!');
  })
  .bind("ajax:beforeSend", function () {
    alert('loading!');
  })
  .bind("ajax:error", function (xhr, status, error) {
    alert('failure!');
  })
  .bind('ajax:success', function(event, data, status, xhr) {
    alert('success!');
  });
});

I did the equivalent in my firebug console and it fired the ajax:success handler when the callback was on the form.

To change the class on the form use the :form_class option to change it from button_to to something else

<%= button_to "remote test", remote_test_path, :remote => true, :class => 'remote_button', :form_class => 'remote_button_form' %>

Then add your ajax callbacks using

jQuery(".remote_button_form").bind ...

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文