:onclick 和 formtastic:第一次单击后禁用提交按钮会阻止表单处理?
我试图确保我的 Rails 应用程序的用户无法通过双击多次提交我的格式表单。
请参阅下面的我的代码。
不幸的是,在将 :onclick 参数添加到提交按钮后,表单不再处理。按钮名称简单更改为禁用按钮“正在处理...”(如提交期间的预期),但此状态是永久的(没有像以前一样的数据验证和重定向)。
我不知道如何调试这个 - 有人可以帮忙吗?
= semantic_form_for @case, :html => {:class => "form-stacked"} do |f|
= f.inputs :name => "Case" do
= f.input :summary, :input_html => {:class => 'xxlarge main_case'}
= f.buttons do
= f.commit_button "Create Case", :button_html => {:class => "btn primary", :onclick => "this.disabled=true; this.value='Processing...';"}
I am trying to make sure the users of my rails app can not submit my formtastic forms multiple times by double clicking.
See my code below.
Unfortunately, after adding the :onclick argument to the commit button, the form does not process anymore. The button name simple changes to a disabled button 'Processing...' (as expected during submission) but this state is permanent (no data validation and redirect as before).
I fail to see how to debug this - can anybody help?
= semantic_form_for @case, :html => {:class => "form-stacked"} do |f|
= f.inputs :name => "Case" do
= f.input :summary, :input_html => {:class => 'xxlarge main_case'}
= f.buttons do
= f.commit_button "Create Case", :button_html => {:class => "btn primary", :onclick => "this.disabled=true; this.value='Processing...';"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试一下:
这将在 Rails 3 及更高版本中工作
更新:
使用 formattastic 您需要以下内容
just try:
this will work in rails 3 and above
Update:
with formtastic you need the following
如果您没有使用 Rails 3,您实际上希望在表单元素上的“onsubmit”上执行此操作,因为在文本字段等中按 Enter 键可以触发提交事件。
确保在 js 代码中“返回 true”,以便在禁用按钮后实际提交表单。
If you are not using rails 3, you would actually want to do that on "onsubmit" on the form element, since pressing enter keys in text fields etc can trigger submit event.
Make sure you "return true" in your js code for the form to actually submit after you disable the button.