繁体   English   中英

使用 js/jquery 在 Rails 上的 ruby 中隐藏 form_tag 上的提交按钮

[英]Hide submit button on form_tag in ruby on rails with js/jquery

点击后有没有办法在rails表单中隐藏提交标签?

我有一个带有提交标签按钮的简单表单标签,它的内容只能提交一次。 因此,在用户最初单击提交按钮以发送表单标签中的数据后,我想隐藏该按钮,以便用户无法再次单击它。

我尝试将它包装在一个 div 中并将 id 传递给 jquery function,它隐藏按钮一秒钟,直到页面重新加载(在完成将数据发送到表单之后),但随后按钮立即重新出现。 我们欢迎所有的建议!

编辑:

用户已登录功能,这是表单

<% form_tag matches_update_path method: :post%>
<% submit_tag “Make Matches?” %> 
<% end %>

本质上,我想在用户单击一次匹配后在这种情况下隐藏此表单/按钮

也许不是ZD223E143918E478349D52476506C22EZ您正在寻找的答案(因为我不确定是否存在),一旦您通过ZD223E1439188E478349D5247652476506C22EZ和Page Rorcrioad the ZD2223EN7911111111111111111111111494444491444444444444444444444444449.11144444444449。按钮首先使用您的 erb 视图,这里有多个选项:

如果数据库中的记录包含user_id并且您可以在 controller 中检查该特定用户是否已提交表单,请在您的 controller 中填写实例值,例如@already_submited_form = true (true if already submitted false if not)然后在erb视图中检查@already_submited_form是否是,然后不显示按钮

<% form_tag matches_update_path method: :post%>
  <% unless @already_submited_form %>
    <% submit_tag “Make Matches?” %> 
  <% end %>
<% end %>

if you can't check for a record in the DB then maybe try using cookies in the rails controller: when the user submits their forms and the request hits the controller set a permanent cookie in the browser cookies.permanent[:already_submited_form] = true然后重用上面的逻辑@already_submited_form = cookies[:already_submited_form]

cookies 在导轨中: https://api.rubyonrails.org/v5.2.1/classes/ActionDispatch/Cookies.html

如果请求没有命中轨道 controller 你也可以在你的 js 代码中设置一个 cookie

如果您仍想使用 jquery 来隐藏它,也许您可以使用这两种方法之一将数据字段放入通用的提交按钮(data-hide-on-page-load=true)然后 jquery 元素数据并隐藏它们(但这似乎有点不必要,因为您可以使用 erb 完成所有这些操作,这是在轨道上使用 ruby 的优势之一)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM