简体   繁体   English

如何在Rails中将collection_select中的值传递给onchange函数?

[英]How do you pass in the value from collection_select to onchange function in Rails?

I'm trying to pass in the value selected from my collection_select drop down to an onchange function. 我试图将从collection_select下拉列表中选择的值传递给onchange函数。 When I do name, my value prints out as source[index] , but I want the value of this not that as the text itself. 当我命名时,我的值会以source [index]的形式打印出来,但我希望此值不是文本本身。

<%= 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);
}

Try this: 尝试这个:

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

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

Changing the argument from "name" to "this.value" solved my problem. 将参数从“名称”更改为“ 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);
}

I think it would be an example in below: 我认为这将是以下示例:

<%= 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

Hope will help you 希望能帮到您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM