Ruby on Rails: Submit a form as a JS based on a change in select box

后端 未结 3 666
悲&欢浪女
悲&欢浪女 2020-12-21 16:23

I have the following HTML generated by form_tag and select_tag in Rails:

相关标签:
3条回答
  • 2020-12-21 16:31

    Try to repliace 'application.js' by 'application.js.coffee' and add #= require_self in the file.

    0 讨论(0)
  • 2020-12-21 16:39

    If you give an id to your <form> tag you can use the id there.

    Like:

    <form accept-charset="UTF-8" action="/deployment_group/show_workflow_list" data-remote="true" id="this_form" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div>
    <select id="folder_name_foldernames" name="folder_name[foldernames]"><option value="RISK_ODS" selected="selected">RISK_ODS</option>
        <option value="DETAIL_ADJUSTMENT">DETAIL_ADJUSTMENT</option>
        <option value="ODS_STAGE">ODS_STAGE</option>
        <option value="FINANCE_DM1">FINANCE_DM1</option>
    </select>
    

    and then:

    $('#folder_name_foldernames').change(function(){
        $('#this_form').submit();
    });
    

    Don't forget to wrap your selectors in quotes or you'll get a JavaScript error.

    0 讨论(0)
  • 2020-12-21 16:42

    I solved the problem doing it the following way:

    <%= f.select :foldernames, options_for_select(@folders, @folders.first), {}, {:onchange=>"myfunc()"}%>
    

    and i added

    <script type="text/javascript">
    function myfunc()
    {
        $('#folder_name_foldernames').change(function(){     $('#this_form').submit(); }); 
    }
    </script>
    

    and the form is being submitted as JS instead of HTML. It would be very helpful if someone can explain me why this is being submitted as JS instead of HTML The same code when i write :onchange=> this.form.submit, is being submitted as HTML

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