当用户提交时,我们如何隐藏相应的表单?
<% @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"
发布评论