form_forヘルパーで作成したformの一部を部分テンプレートに切り分け、ラジオボタンの値によってjqueryで呼び出す部分テンプレートを切り替える際にsubmitボタンが動作しないことがあります。()
ruby
1<%= form_for @message do |f| %> 2 <%= f.label '質問フォーム' %> 3 <%= f.radio_button :message_type, 1, checked: true %> 4 <%= f.label 'コンタクトフォーム' %> 5 <%= f.radio_button :message_type, 2 %> 6 <div id="question_format"> 7 <%= render partial: 'question', locals: {f:f} %> 8 </div> 9 <div id="contact_format"> 10 <%= render partial: 'contact', locals: {f:f} %> 11 </div> 12 <%= f.submit :submit %> 13<% end %> 14 15<script> 16 $(function(){ 17 $('input[name="message[message_type]"]:radio').change(function(){ 18 var checked_btn_id = $(this).attr("id"); 19 if(checked_btn_id == 'message_message_type_2') { 20 $('#question_format').toggle(false); 21 $('#contact_format').toggle(true); 22 } else { 23 $('#contact_format').toggle(false); 24 $('#question_format').toggle(true); 25 }; 26 }); 27 }); 28</script>
ただ呼び出す部分テンプレートが質問フォーム、コンタクトフォーム問わず稀に動作することもあり困っています。どなたか一緒に考察して下さると助かります。
あなたの回答
tips
プレビュー