Submitting Rails Form on a Radio Button Selection

后端 未结 3 1464
梦毁少年i
梦毁少年i 2021-02-11 06:45

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. How do I do that?

<
相关标签:
3条回答
  • 2021-02-11 07:24

    So I have found the precise solution (thanks for the input guys, it helped me redefine my Google searches). 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. 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 %>
    
    0 讨论(0)
  • 2021-02-11 07:25

    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:

    <% 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.

    0 讨论(0)
  • 2021-02-11 07:34

    You'll want to add an onClick event to your radio button that calls this.formName.submit() where formName is your form ID. This will trigger the submit event when you click the radio button.

    0 讨论(0)
提交回复
热议问题