提交后如何隐藏表格?(How to hide form upon submit?)

编程入门 行业动态 更新时间:2024-10-23 09:27:01
提交后如何隐藏表格?(How to hide form upon submit?)

当用户提交时,我们如何隐藏相应的表单?

<% @challenge.dates_challenged.first(@challenge.days_challenged + @challenge.missed_days).each_with_index do |date, i| %> <div id="show-all-notes"> # upon submit the text appears here, javascript magic </div> <div class="notes-form-background"> DAY <%= i + 1 %> <%= date.strftime("%b %d, %Y") %> <%= form_for [@notable, @note], remote: true do |f| %> <%= f.text_area :notes_text %> <%= f.submit, class: "btn" %> <% end %> </div> <% end %> <script> $(document).ready(function(){ $('.btn').click(function(){ $('form').toggle(); }); }); </script>

有了类似我的脚本,虽然它删除了表单的所有迭代,而不仅仅是提交的那个。

有没有办法使用Day <%= i + 1 %>来唯一标识每个表单,以便只隐藏提交的表单?

When a user submits, how can we hide the respective form?

<% @challenge.dates_challenged.first(@challenge.days_challenged + @challenge.missed_days).each_with_index do |date, i| %> <div id="show-all-notes"> # upon submit the text appears here, javascript magic </div> <div class="notes-form-background"> DAY <%= i + 1 %> <%= date.strftime("%b %d, %Y") %> <%= form_for [@notable, @note], remote: true do |f| %> <%= f.text_area :notes_text %> <%= f.submit, class: "btn" %> <% end %> </div> <% end %> <script> $(document).ready(function(){ $('.btn').click(function(){ $('form').toggle(); }); }); </script>

With something like the script I have though it removes all iterations of the form, not just the one submitted.

Is there maybe a way to use Day <%= i + 1 %> to uniquely identify each form so that only the form that is submitted is hidden?

最满意答案

实现目标的代码中最小的变化可能是隐藏form ,这是单击按钮的最近祖先 :

<script> $(document).ready(function(){ $('.btn').click(function(){ $(this).closest('form').hide(); }); }); </script>

否则,您可以为表单和按钮对生成唯一ID,在按钮单击处理程序中解析ID,格式化相关表单ID并找到要隐藏的相应表单。

The smallest change in your code to achieve your goal would be probably hiding the form, which is the closest ancestor to the button clicked:

<script> $(document).ready(function(){ $('.btn').click(function(){ $(this).closest('form').hide(); }); }); </script>

Otherwise you could generate unique IDs for the pairs of forms and buttons, parse the ID in the button click handler, format the related form ID and find the respective form to hide by it.

更多推荐

<%,%>,电脑培训,计算机培训,IT培训"/> <meta name="description"

本文发布于:2023-07-05 09:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1035493.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表格   提交后   hide   form   submit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!