简体   繁体   English

jQuery隐藏在Rails表单中的提交按钮单击

[英]jquery hide on rails form submit button click

I have the following form in my rails view: 我在rails视图中具有以下形式:

<%= form_for :search do |f| %>
      <%= f.label :search_by_name %>
      <%= f.text_field :by_name  %>
      <%= f.submit 'Search' %>
<% end %>

In jQuery, I can do something like this: 在jQuery中,我可以执行以下操作:

        $("h1").click(function(){
            $("h1").hide();
        });

That would cause all h1 tags to become invisible upon clicking an h1 tag. 这将导致所有h1标签,成为在点击一个无形的h1标签。 How can I instead cause my entire form to become invisible by clicking on the "submit" button for my form above? 通过单击上方表单的“提交”按钮,如何使整个表单不可见?

EDIT 编辑

Perhaps the issue is a little bigger than I'm thinking. 也许这个问题比我想的要大。 When I do this per the answer below: 当我按照以下答案进行操作时:

 $("form").submit(function(){
        $(this).hide();
    })

The form becomes invisible, but then the page is immediately reloaded and the form is visible again. 表单变得不可见,但是随后页面立即被重新加载,并且表单再次可见。 It stays invisible for a split second before reloading the page. 在重新加载页面之前,它会在一秒钟内保持不可见。 Is there a way to carry that affect over from page to page, or to display the form results without reloading the page? 有没有一种方法可以在页面之间传递影响,或显示表单结果而无需重新加载页面?

Edit 2 编辑2

Problem solved. 问题解决了。 I wrapped the form in a div class and passed that ID into the hide function as mentioned in the comments below, and that seems to work. 我将表单包装在div类中,并将该ID传递到hide函数中,如下面的注释中所述,这似乎可行。 I don't understand why the page was just realoading and displaying the form when passing in form , but it doesn't have that effect when passing in a id . 我不明白为什么页面在传递表单时只是重新加载并显示form ,但是在传递id时却没有这种效果。 But, problem solved. 但是,问题解决了。

Try 尝试

 $("form").submit(function(){
        $(this).hide();
    })

Seems to me that Ajax is what you want (like @arieljuod said) but if you just wanted to hide the form without passing data to the server you could have prevented the event. 在我看来,Ajax是您想要的(就像@arieljuod所说的),但是如果您只想隐藏表单而不将数据传递到服务器,则可以避免该事件。

$("form").submit(function(e){
    e.preventDefault();
    $(this).hide();
})

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

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