Rails 中 select 的 onchange 传递参数

yer*_*syl 0 javascript jquery ruby-on-rails

<%= f.select :id, options_from_collection_for_select(
              @rtypes, "id", "typeName"), 
              {include_blank: true },																				                            
:onchange => ShowSubTypes() %>	
Run Code Online (Sandbox Code Playgroud)

这里我有 select 的选项,我想要的是将 id 作为参数传递给ShowSubTypes()函数。我的@rtypes变量返回一个数组,因此我不能只传递@rtypes.id.

Tho*_*uah 5

<%= f.select :id, options_from_collection_for_select(
              @rtypes, "id", "typeName"), 
              {include_blank: true },                                                                                                           
:onchange => "ShowSubTypes($(this).val())" %>   
Run Code Online (Sandbox Code Playgroud)

基本上你可以通过 JavaScript 来完成这个任务。thisonChange 的上下文中是<select>输入。我假设 jQuery 获取与 @rtypes 的“id”相对应的所选选项的值。

在简单的 Javascript 中,我认为它是this.value