Ruby on Rails 表单中的 select 和 onChange
我浏览了有关此主题的所有问题和答案,但我仍然无法使我的场景发挥作用。 我想在选择下拉菜单选项时触发单击按钮操作;看起来很简单,在 AJAX 中应该很常见。
以下是我的代码的相关摘录:
<%= 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 %>
我对表单和按钮都使用了 ":remote => "true" 因为这是让 AJAX 工作的唯一方法。我还尝试过使用和不使用显式的 "html_options" 和 "javascript :",在我浏览了一些建议的答案后,但这没有帮助。我也尝试了 onSelect 和 onClick 而不是 onChange,但仍然没有运气。
生成的 HTML 如下:
Menu1
<select id="test_Menu1" name="test[Menu1]"><option value="value1">Option1</option>
<option value="value2" selected="selected">Option2</option></select>
如您所见,没有HTML 代码中的 onChange 事件处理程序;为什么?有人看到我做错了什么吗?
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修改对
form.select
的调用,如下所示:Modify your call to
form.select
, like this:如果您检查以下文档:
API Dock Ruby on Rails select
您将看到选择表单助手采用以下形式:
select(object, method, choice, options = {}, html_options = {})
如果您没有为选项哈希传递任何内容(在您的情况下,这将是一个空哈希),该表单认为您的 html_options 哈希是您的选项哈希,并且会感到困惑。
检查这一点的一种方法是添加类似 {:onchange=>; "alert('Hello');"} 并查看事件是否成功触发,或者在您的实际网页中,右键单击选择元素并检查它。如果 html 中不存在 onchange 选项,则意味着您的 Rails 表单助手确实将 html_options 与其他选项混淆了。因此,您应该具备:
请确保在 HTML 选项之前包含选项的空哈希,这样就可以了。我认为你甚至不需要拥有 html_options 和 javascript 的东西。
最后,如果onChange不起作用,请尝试使用不带大写C的onchange。
If you examine the documentation for:
API Dock Ruby on Rails select
You will see that the select form helper takes the form:
select(object, method, choices, options = {}, html_options = {})
If you don't pass anything for the option hash (in your case this will be an empty hash), the form thinks that your html_options hash are your options hash, and gets confused.
A way to check this is to add something like {:onchange=> "alert('Hello');"} and either see if the event successfully triggers, or alternatively, in your actual web page, right click on the select element and inspect it. If no onchange option is present in the html, that means that your rails form helper is indeed confusing the html_options with the other options. So, what you should have:
MAKE SURE TO INCLUDE THE EMPTY HASH FOR THE OPTIONS BEFORE THE HTML OPTIONS AND YOU SHOULD BE FINE. I don't think you even need to have the html_options and javascript stuff you have.
Lastly, if onChange doesn't work, try to use onchange with no capital C.