collection_radio_button自动提交?

时间:2016-03-05 20:11:19

标签: javascript ruby-on-rails ruby ruby-on-rails-4

有没有办法点击collection_radio_button表单会自动提交?

这样他们就不必单击自动发生的提交按钮。

<%= simple_form_for(@challenge) do |f| %>
  <%= f.text_field :action %>
    Or choose a featured challenge:
  <%= f.collection_radio_buttons :action, [['Lose 10 Pounds','Lose 10 Pounds'], ['Exercise','Exercise'], ['Quit Smoking','Quit Smoking'], ['Run a 5K','Run a 5K'], ['Meditate','Meditate'], ['Visit Rome','Visit Rome'], ['Read a Chapter','Read a Chapter'], ['Walk 10,000 Steps','Walk 10,000 Steps'], ['Study a Foreign Language','Study a Foriegn Language'], ['No T.V.','No T.V.'], ['Backpack through Europe','Backpack through Europe'], ['Journal','Journal'], ['Go Vegetarian','Go Vegetarian']], :first, :last %>
  <%= f.submit %>
<% end %>

2 个答案:

答案 0 :(得分:3)

你可以试试这个......

$('input[type=radio]').on('change', function() {
    $(this).closest("form").submit();
});

此处使用属性选择器,您可能希望将其更改为类或更符合您需求的内容。

答案 1 :(得分:0)

您可以使用onchange: "this.form.submit();"

<%= f.collection_radio_buttons :action, [['Lose 10 Pounds', bla bla bla], :first, :last, onchange: "this.form.submit();" %>