简体   繁体   English

在单选按钮选择上提交Rails表单

[英]Submitting Rails Form on a Radio Button Selection

I have the following rails form (which works) but I want to remove the submit_tag and have the form submit as soon as a radio button selection is made. 我有以下rails表单(有效)但我想删除submit_tag并在选择单选按钮后立即提交表单。 How do I do that? 我怎么做?

<% form_tag({ :controller => "votes", :action => "create", :id => @item.id } ) do  %>
  <p>
    <% for i in 1..10 do %>
      <%=  radio_button_tag :rating, i %>&nbsp;<%=h i %>&nbsp;
    <% end %>
  </p>
  <p>
    <%= submit_tag "Create" %>
  </p>
<% end %>

So I have found the precise solution (thanks for the input guys, it helped me redefine my Google searches). 所以我找到了精确的解决方案(感谢输入人员,它帮助我重新定义了我的Google搜索)。 Everyone was on the right track, but none were spot on. 每个人都在正确的轨道上,但没有一个是现场。

All that was needed was to add 'false, :onclick => "this.parentNode.submit();"' to the radio_button_tag form helper. 所需要的只是将'false,:onclick =>“this.parentNode.submit();”添加到radio_button_tag表单助手中。 The false is selected or not, and the rest is obvious. 是否选择了错误,其余的是显而易见的。

The correct solution now looks like: 现在正确的解决方案如下:

<% form_tag({ :controller => "votes", :action => "create", :id => @item.id } ) do  %>
    <% for i in 1..10 do %>
      <%=  radio_button_tag :rating, i, false, :onclick => "this.parentNode.submit();" %>&nbsp;<%=h i %>&nbsp;
    <% end %>
<% end %>

I am doing something similar so this question really helps. 我正在做类似的事情所以这个问题确实有帮助。

Further to Ash's answer, I found that the above onclick function does not work, I have changed a bit and the form below work just fine to me: 除了Ash的答案之外,我发现上面的onclick函数不起作用,我已经改了一点,下面的表格对我来说还不错:

<% form_tag({ :controller => "votes", :action => "create", :id => @item.id } ) do  %>
    <% for i in 1..10 do %>
      <%=  radio_button_tag :rating, i, false, :onclick => "this.form.submit();" %>&nbsp;<%=h i %>&nbsp;
    <% end %>
<% end %>

Hope it would help somebody. 希望它会对某人有所帮助。

You'll want to add an onClick event to your radio button that calls this.formName.submit() where formName is your form ID. 您需要将onClick事件添加到调用this.formName.submit()的单选按钮,其中formName是您的表单ID。 This will trigger the submit event when you click the radio button. 单击单选按钮时,这将触发提交事件。

Use Javascript 使用Javascript

Use each radio button's onclick() event to call a function that calls submit() on the submit button. 使用每个单选按钮的onclick()事件来调用在提交按钮上调用submit()的函数。

I'm not really familiar with RJS in Rails, but this should be relatively easy. 我对Rails中的RJS并不熟悉,但这应该相对简单。

An awesome way to achieve this unobtrusively is with lowpro and prototype. 实现这一目标的一种很棒的方法是使用lowpro和prototype。 That way you can get all your event handlers out into your javascript so they don't pollute your view code. 这样您就可以将所有事件处理程序都放到javascript中,这样它们就不会污染您的视图代码。 It is a very slick way of handling javascript events. 这是处理javascript事件的一种非常流畅的方式。 Highly recommended. 强烈推荐。

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

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