将参数传递给Rails中的onchange事件
我的rails应用程序中有一个select标签,我想调用showSubTypes函数函数onchange事件
'showSubTypes()' %>
在这里,我想将p.id参数传递给我的showSubtypes函数。 但我不知道如何在rails中做到这一点。
例如,您可以在application.js
添加此代码
$(function () { // specify id or class for your select tag $('select').on('change', function () { console.log($(this).val()); // get option value console.log($(this).text()); // get option text showSubTypes($(this).val()); }); });
如果使用jQuery,则不需要使用内联事件
并在您的模板中
<%= f.select :type, RequestType.all.collect {|p| [p.typeName, p.id]}, {include_blank: true } %>