Ruby on Rails:基于选择框的更改,以JS形式提交表单 [英] Ruby on Rails: Submit a form as a JS based on a change in select box
问题描述
我在Rails中具有form_tag
和select_tag
生成的以下HTML:
I have the following HTML generated by form_tag
and select_tag
in Rails:
<form accept-charset="UTF-8" action="/deployment_group/show_workflow_list" data-remote="true" id="show_workflow_list_form" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></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>
</form>
让我知道我需要编写的JQuery代码,以便我可以在下拉列表中的选择值更改时提交表单.请参阅此处,当我使用:onchange时,这是一个html选项,该表单以html而不是JS表单的形式提交.所以,我不能使用:onchange.
Let me know the JQuery code i need to write so that i can submit the form on change of a select value in the drop down. Please see here that when i use :onchange, it is a html option the form is submitted as html and not as a JS form. So, i cannot use :onchange.
我了解到我需要在application.js文件中编写如下内容:
I understood that i need to write something like this in my application.js file:
$(#folder_name_foldernames).change(function(){
$(#show_workflow_list).submit()
});
我在application.js文件中使用了上面的代码 但是我必须删除"require_tree".当我得到一些错误 所以我加了
I used the above code in application.js file but i have to remove "require_tree ." as i get some error so i added
$('#folder_name_foldernames').change = () ->
$('#show_workflow_list_form').submit()
到我的deployment_group.js.coffee文件中(deployment_group是与上述视图相关的我的控制器)
to my deployment_group.js.coffee file (the deployment_group is my controller related the above view)
但是当我在下拉列表中更改选择时,我仍然看不到任何变化. 请帮忙.
But still i dont see any change when i change the selection in the drop down. Please help.
推荐答案
我通过以下方式解决了该问题:
I solved the problem doing it the following way:
<%= f.select :foldernames, options_for_select(@folders, @folders.first), {}, {:onchange=>"myfunc()"}%>
我添加了
<script type="text/javascript">
function myfunc()
{
$('#folder_name_foldernames').change(function(){ $('#this_form').submit(); });
}
</script>
,并且表单以JS而不是HTML的形式提交.如果有人可以向我解释为什么将其作为JS而不是HTML提交,这将非常有帮助. 当我写:onchange => this.form.submit时,相同的代码正在以HTML格式提交
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
这篇关于Ruby on Rails:基于选择框的更改,以JS形式提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!