在Rails的AJAX形式prevent双的提交 [英] Prevent double submits in a Rails AJAX form

查看:155
本文介绍了在Rails的AJAX形式prevent双的提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我通过AJAX提交表单。

I have a form that I submit via AJAX.

<%= form_for([@map, @annotation], :remote => true ) do |f| %>
   ...
   <%= f.submit "Save annotation", :class => "btn btn-primary", :id => "annotation-submit-button" %>
<% end %>

我要什么:

我想prevent双重的提交。由于该表格只消失时,请求成功完成,用户可以点击,只要数据库没有写完的数据提交按钮。我不希望出现这种情况,很明显。

What I want:

I would like to prevent double submits. Since the form only disappears when the request has successfully completed, users can click the submit button as long as the database hasn't finished writing the data. I don't want that, obviously.

我尝试添加这样的提交按钮本身 - 但它不工作。该按钮被禁用,但没有数据被发送。

I tried adding this to the submit button itself – but it doesn't work. The button gets disabled, but no data is sent.

:onclick => "this.disabled = true"

我也尝试添加一个单击处理程序提交按钮。这和以前一样具有相同的效果。没有数据被实际发送到所述控制器,但按钮被禁用。

I've also tried adding a click handler to the submit button. This has the same effect as before. No data is actually sent to the controller, but the button is disabled.

$("#annotation-submit-button").click(function(event) {
  $(this).attr("disabled", "disabled");
  return false;
});

也试过同样不返回。被禁用按钮后没有任何反应。

Also tried the same without returning false. Nothing happens after the button is disabled.

$("#annotation-submit-button").click(function(event) {
  $(this).prop("disabled", "disabled");
});

我开始认为这是值得的Rails的具体情况?

I begin to think that this is something Rails-specific?

推荐答案

尝试的 disable_with =nofollow的> <$ C $ 在这样的视图的Rails 3及以上的:

Try disable_with in the view like this for Rails 3 and above:

<%= f.submit "Save annotation", :data => {:disable_with => "Saving..."}, ... %>

有关轨道2:

<%= f.submit "Save annotation", :disable_with => "Saving...", ... %>

这篇关于在Rails的AJAX形式prevent双的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆