2016-03-07 62 views
1

我想将从我的collection_select下拉列表中选择的值传递给onchange函数。当我的名字,我的价值打印为来源[指数],但我想这不是作为文本本身的价值。你如何将值从collection_select传递给Rails中的onchange函数?

<%= collection_select(:source, :index, @sources, :id, :name, options = {include_blank: "Please select a source..."}, html_options = {:onchange => "updateTextArea(name)"}) %> 

function updateTextArea(source){ 
var value = source; 
console.log(value); 
} 

回答

0

我认为这将是一个例子如下:

<%= f.collection_select(:id, index.sources, :id, :name,options = {include_blank: "Please select a source..."}, html_options = {:onchange => "updateTextArea(name)"}) %> #-> f is a form tag this you use this form 

希望能帮助你

+0

如果我不使用表格?这是一个独立的下降。 – A21

+0

好吧,试试不用'f' –

+0

这就是我在我的问题中所做的。我可以让下拉菜单正确显示。但是,传递给onchange函数的值不会打印出在下拉列表中选择的值。 – A21

1

试试这个:

html_options = {:onchange => "updateTextArea()"} 

function updateTextArea() { 
    console.log($(this).val()); 
} 
+0

,给我以下错误 - 未定义的局部变量或方法'名称' – A21

+0

请参阅我的回答的更新版本 –

+0

仍然没有运气 - 未捕获TypeError:无法读取属性未定义的'toLowerCase' – A21

2

从 “名” 来更改参数“this.value”解决了我的问题。

<%= collection_select(:source, :index, @sources, :id, :name, options = {include_blank: "Please select a source..."}, html_options = {:onchange => "updateTextArea(this.value)"}) %> 

function updateTextArea(source){ 
var value = source; 
console.log(value); 
}