在Ruby on Rails窗体中选择并打开onChange [英] select and onChange in a Ruby on Rails form
问题描述
我浏览了所有关于这个主题的问题和答案,但我仍然无法让自己的方案奏效。
我想在选择下拉菜单选项时触发单击按钮操作;看起来很简单,应该在AJAX中很常见。
以下是我的代码的相关摘录:
<$ p $ <@ c $ c><%= form_for(@test,:html => {:id =>form_id,:name =>MyForm,:remote =>true} )do | form | %GT;
<%= form.labelMenu1%>
<%= form.select(:Menu1,[[Option1,value1],[Option2,value2]],:html_options => {:onChange =>javascript :this.form.apply_button_name.click();})%>
<! - 更多选择菜单和文本字段 - >
< div class =actions>
<%= form.submitApply,:name => apply_button_name,:remote => true%>
< / div>
<%end%>
我对表单和按钮使用了:remote =>true,因为这是唯一的我也尝试过使用和不使用明确的html_options和javascript:,在我浏览了一些SO的答案之后,我发现它并没有帮助,我也尝试过选择,onClick而不是onChange,但仍然
生成的HTML如下:
Menu1
< select id =test_Menu1name =test [Menu1]>< option value =value1> Option1< / option>
< option value =value2 =selected> Option2< / option>< / select>
正如你所看到的,在HTML代码中没有onChange事件处理程序;为什么?任何人都看到我做错了什么?
感谢您的帮助。
<解决方案将您的调用修改为
form.select
,如下所示: <%= form.se lect:Menu1,[[Option1,value1],[Option2,value2]],{},
:onChange =>javascript:this.form.apply_button_name.click() ;%>
I browsed all SO questions and answers about this topic but I'm still unable to make my scenario work. I want to trigger a click button action when a dropdown menu option is selected ; seems simple and should be very common with AJAX.
Here are the relevant excerpts of my code:
<%= form_for(@test, :html => {:id => "form_id", :name => "MyForm", :remote => "true"}) do |form| %>
<%= form.label "Menu1" %>
<%= form.select (:Menu1, [["Option1","value1"],["Option2","value2"]], :html_options=>{:onChange=>"javascript: this.form.apply_button_name.click();"}) %>
<!-- more select menus and text fields here -->
<div class="actions">
<%= form.submit "Apply", :name => "apply_button_name", :remote => "true" %>
</div>
<% end %>
I used ":remote => "true" both for the form and the button because that's the only way to get AJAX working. I also tried with and without explicit "html_options" and "javascript:", after I browsed some SO answers that suggested that but that did not help. I also tried onSelect, and onClick instead of onChange, but still no luck.
The generated HTML is the following:
Menu1
<select id="test_Menu1" name="test[Menu1]"><option value="value1">Option1</option>
<option value="value2" selected="selected">Option2</option></select>
As you can see, there's no onChange event handler in the HTML code ; WHY? Anyone is seeing what am I doing wrong?
Thanks for any help.
Modify your call to form.select
, like this:
<%= form.select :Menu1, [["Option1","value1"],["Option2","value2"]], {},
:onChange=>"javascript: this.form.apply_button_name.click();" %>
这篇关于在Ruby on Rails窗体中选择并打开onChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!