如何禁用表单提交按钮“la Ruby on Rails Way”?

时间:2022-11-24 11:05:23

I am using Ruby on Rails 3 and I would like to disable and toogle the CSS class of a form.submit when the form is AJAX submitted and until the AJAX HTTP request is completed (I am using the default jQuery framework for JavaScript).

我正在使用Ruby on Rails 3,我想禁用和toogle一个表单的CSS类。在提交表单时提交,直到AJAX HTTP请求完成(我使用的是JavaScript的默认jQuery框架)。

My form is the following:

我的表格如下:

<%= form_for(@article, :remote => true) do |form| %>
    ...
    <%= form.submit(nil, {:id => 'button_id', :class => 'button_class'}) %>
<% end %>

How can I make that in a "common"/"good"/"proper" way?

我怎样才能用“普通”/“好”/“适当”的方式来表达呢?

1 个解决方案

#1


43  

The Rails jQuery bridge (jquery_ujs.js) code actually has a helper for this.

Rails jQuery bridge (jquery_ujs.js)代码实际上为此提供了一个助手。

<%= form.submit "Save", id: "button_id", class: "button", disable_with: "Submitting..."

It will replace the button text with the value you give.

它将用您提供的值替换按钮文本。

See http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-submit_tag

看到http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html method-i-submit_tag

API change: as Renan suggests in a comment below, as of Rails 4 beta there is a deprecation notice on the disable_with option. It should be changed to be a data attribute:

API更改:正如Renan建议的,在下面的注释中,Rails 4 beta中有一个关于disable_with选项的弃用通知。应将其更改为数据属性:

<%= form.submit "Save", id: "button_id", class: "button", data: {disable_with: "Submitting..."} %>

This should work with all recent versions of Rails as it's what the option did anyway. So it'll be one less deprecation notice to fix when you upgrade to Rails 4. :)

这应该适用于所有最近版本的Rails,因为它是该选项所做的。因此,当您升级到Rails 4时,它将是一个减少弃用的通知。:)

#1


43  

The Rails jQuery bridge (jquery_ujs.js) code actually has a helper for this.

Rails jQuery bridge (jquery_ujs.js)代码实际上为此提供了一个助手。

<%= form.submit "Save", id: "button_id", class: "button", disable_with: "Submitting..."

It will replace the button text with the value you give.

它将用您提供的值替换按钮文本。

See http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-submit_tag

看到http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html method-i-submit_tag

API change: as Renan suggests in a comment below, as of Rails 4 beta there is a deprecation notice on the disable_with option. It should be changed to be a data attribute:

API更改:正如Renan建议的,在下面的注释中,Rails 4 beta中有一个关于disable_with选项的弃用通知。应将其更改为数据属性:

<%= form.submit "Save", id: "button_id", class: "button", data: {disable_with: "Submitting..."} %>

This should work with all recent versions of Rails as it's what the option did anyway. So it'll be one less deprecation notice to fix when you upgrade to Rails 4. :)

这应该适用于所有最近版本的Rails,因为它是该选项所做的。因此,当您升级到Rails 4时,它将是一个减少弃用的通知。:)