简体   繁体   English

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

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

Is there a way to hide a submit tag in a rails form after it's been clicked?点击后有没有办法在rails表单中隐藏提交标签?

I have a simple form tag with a submit tag button, which has content that should only be submitted once.我有一个带有提交标签按钮的简单表单标签,它的内容只能提交一次。 So, after a user initially clicks the submit button to send the data in the form tag, I want to hide the button so users cannot click it again.因此,在用户最初单击提交按钮以发送表单标签中的数据后,我想隐藏该按钮,以便用户无法再次单击它。

I tried wrapping it in a div and passing the id into a jquery function, and it hides the button for one second until the page reloads (after it finishes sending the data to the form) but then the button immediately reappears.我尝试将它包装在一个 div 中并将 id 传递给 jquery function,它隐藏按钮一秒钟,直到页面重新加载(在完成将数据发送到表单之后),但随后按钮立即重新出现。 Any suggestions would be much appreciated!我们欢迎所有的建议!

Edit:编辑:

The user has logged in functionality and this is the form用户已登录功能,这是表单

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

Essentially I want to hide this form/button in this case after the user clicks make matches once本质上,我想在用户单击一次匹配后在这种情况下隐藏此表单/按钮

Maybe not the jquery answer you are looking for (cause I'm not sure there is one), once you hide something via jquery and the page reload that jquery should re-run in order to hide the button again or you can not show the button in the first place using your erb view there are multiple options here:也许不是ZD223E143918E478349D52476506C22EZ您正在寻找的答案(因为我不确定是否存在),一旦您通过ZD223E1439188E478349D5247652476506C22EZ和Page Rorcrioad the ZD2223EN7911111111111111111111111494444491444444444444444444444444449.11144444444449。按钮首先使用您的 erb 视图,这里有多个选项:

if the record in the DB includes a user_id and you can check in your controller that this particular user already submitted the form fill a instance value in your controller eg @already_submited_form = true (true if already submitted false if not) then in the erb view check if for the @already_submited_form if it was then don't show the button如果数据库中的记录包含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 then reuse the logic from above @already_submited_form = cookies[:already_submited_form] 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 in rails: https://api.rubyonrails.org/v5.2.1/classes/ActionDispatch/Cookies.html cookies 在导轨中: https://api.rubyonrails.org/v5.2.1/classes/ActionDispatch/Cookies.html

if the request does not hit a rails controller you can also set a cookie in your js code如果请求没有命中轨道 controller 你也可以在你的 js 代码中设置一个 cookie

if you still want to use jquery to hide this maybe you can use one of these 2 methods to put a data field into the submit button which is generic (data-hide-on-page-load=true) then jquery the elements with that data and hide them (but it seems a bit unnecessary since you can do all this using erb which is one of the advantages of using ruby on rails)如果您仍想使用 jquery 来隐藏它,也许您可以使用这两种方法之一将数据字段放入通用的提交按钮(data-hide-on-page-load=true)然后 jquery 元素数据并隐藏它们(但这似乎有点不必要,因为您可以使用 erb 完成所有这些操作,这是在轨道上使用 ruby 的优势之一)

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

相关问题 Ruby on Rails 2.3,form_tag提交给Rails控制器,未调用JS函数 - Ruby on Rails 2.3, form_tag submitted to Rails controller, JS function not called 使用JavaScript验证使用form_tag的输入字段时,提交按钮停止在rails中工作 - Submit button stops working in rails when using javascript to validate input fields when using form_tag 使用javascript在rails 3中提交远程form_tag,但提交为html - submit a remote form_tag in rails 3 using javascript but submits as html Rails form_tag提交数据而无需重新加载整个页面 - Rails form_tag submit data without reload the whole page Ruby on Rails:无法从form_tag调用ajax回调 - Ruby on Rails:Can not call ajax callback from form_tag 在Ruby on Rails中使用JavaScript创建和删除form_tag - Create and remove a form_tag with JavaScript in Ruby on Rails 与提交Rails表单相比,让JQuery / JS在完成后提交Ruby on Rails按钮? - Having JQuery/JS submit a Ruby on Rails button after finishing, as compared to submitting a rails form? Rails Form_Tag Ajax Format.js ActionController :: UnknownFormat - Rails Form_Tag Ajax Format.js ActionController::UnknownFormat Ruby on Rails-form.submit()jQuery不将参数作为JS传递 - Ruby on Rails - form.submit() Jquery not passing parameters as JS Rails form_tag远程以另一种形式 - Rails form_tag remote in another form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM