在Rails的AJAX形式prevent双的提交形式、Rails、AJAX、prevent

2023-09-10 13:43:25 作者:哥い很霸道

我有我通过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的>