Javascript 如何将onchange操作添加到集合\u select?

Javascript 如何将onchange操作添加到集合\u select?,javascript,ruby-on-rails,Javascript,Ruby On Rails,我在rails中有以下选择下拉列表。我遵循API()中的语法: 集合\u选择(对象、方法、集合、值\u方法、文本\u方法、选项={}、html\u选项={}) “请选择一个源…”,html\u选项={:onchange=>“updateTextArea()”})%> 函数updateTextArea(){ 警报('源已更改') } 当我不包含html_选项时,我能够使用DB中的值将下拉列表显示得很好。但是,我一直在尝试执行一个onchange操作。我认为选项需要在散列中(您当前拥有的部分包括\

我在rails中有以下选择下拉列表。我遵循API()中的语法:

集合\u选择(对象、方法、集合、值\u方法、文本\u方法、选项={}、html\u选项={})

“请选择一个源…”,html\u选项={:onchange=>“updateTextArea()”})%>
函数updateTextArea(){
警报('源已更改')
}

当我不包含html_选项时,我能够使用DB中的值将下拉列表显示得很好。但是,我一直在尝试执行一个onchange操作。

我认为选项需要在散列中(您当前拥有的
部分包括\u blank
)。试着改变这个

<%= collection_select(:sources, :source_id, Source.all, :id, :name, :include_blank => "Please select a source...", html_options = {:onchange => "updateTextArea()"} ) %>
“请选择一个源…”,html\u选项={:onchange=>“updateTextArea()”})%>
对此

<%= collection_select(:sources, :source_id, Source.all, :id, :name, options = {include_blank: "Please select a source..."}, html_options = {:onchange => "updateTextArea()"} ) %>
“updateTextArea()”})%>

我认为代替
options=
html\u options=
您需要传递实际的散列本身(就像您实际对
include\u blank=>true所做的那样)。我只会显式地用大括号标记这些散列来分隔它们:

<%= collection_select(:sources, :source_id, Source.all, :id, :name, { :include_blank => "Please select a source..."}, {:onchange => "updateTextArea()"} ) %>

这让我通过了语法异常,但是它没有给我一个警告对话框,所以我不确定为什么在我更改下拉值时函数没有被触发。我的错误是,我的标记没有关闭。现在它也在相应地工作。
<%= collection_select(:sources, :source_id, Source.all, :id, :name, { :include_blank => "Please select a source..."}, {:onchange => "updateTextArea()"} ) %>
window.updateTextArea = function() { /* Your code */ }