假设我在视图中有以下rails代码:
<%= form.select(:language_id,
options_from_collection_for_select(Language.all, :id, :name),
{},
{ :onchange => "setThemeAndMode('theme', 'mode')", class: "form-control" })
%>
是否可以在onchange事件中向language.theme
调用发送language.mode
和setThemeAndMode
?或者甚至将特定的language
模型元素作为一些扁平的json?
答案 0 :(得分:2)
我很难按照你的意愿行事,但这里有一个我认为你在问的东西。
如果您知道渲染时的值,则可以插入onchange字符串。
char* widen_stars(char * str1, char *str2)
{
int ls1 = strlen(str1);
int ls2 = strlen(str2);
char *str4 = (char *) malloc(strlen(str1) + strlen(str2)+ 29);
char *p1 = str1;
char *p2 = str2;
char *p3 = str4;
char *asterix;
*asterix = '*'; //Pointer pointing to *, won't let me though//
while(*p1 != '\0'){
*p3 = *p1;
p1++;
p3++;
*p3 = *asterix; // Needs to add an *, after printing the first element.//
}
while(*p2 != '\0'){
*p3 = *p2;
p2++;
p3++;
*p3 = *asterix;
}
return str4;
}
如果您尝试获取select元素新更改的值的值,则需要在javascript中获取select元素的值。
<%= form.select(:language_id,
options_from_collection_for_select(Language.all, :id, :name),
{},
{ :onchange => "setThemeAndMode('#{Mything}', '#{other thing}')", class: "form-control" })
%>
答案 1 :(得分:1)
正如您在Rails's document中所看到的,您可以将块传递给select
,以自定义选项标记的呈现方式。
form.select :language_id do
Language.all.each do |lang|
content_tag(:option, c.name, value: c.id,
onchange: "setThemeAndMode('#{get_theme_logic}', '#{get_mode_logic}')")
end
end
您还可以定义帮助程序以呈现content_tag
以使代码更清晰