0

这是我的代码,在此我想在某些条件下调用 java 脚本函数,例如如果 a = 1 比调用 hi() 函数和 a = 2 比调用 hello() 函数

<%= f.collection_select :ser_id, @ser, :id, :name, :prompt => true -%>

Javascript函数是

function hi()
{
alert("hi");
}

function hello()
{
alert("hello");
}

所以现在给我一些想法,我怎样才能根据给定的条件调用这些函数。

4

1 回答 1

1

尝试 :

<% fun_name = "" %>
<% fun_name = (a==1 ? "eval('hi()')" : "eval('hello()')") if a==1 || a==2 %>
<%= f.collection_select :ser_id, @ser, :id, :name, :prompt => true, {:onchange => fun_name } -%>
于 2012-04-06T10:51:11.780 回答