我对javascript很陌生,但我知道你可以调用一个函数,它由一个字符串表示,这样:
var function_to_call = window[values['function']];
//Where values['function']='functionName'
到目前为止一切顺利,那么我们有:
if(typeof function_to_call == 'function'){
if(values['parameters']!= '')
function_to_call(values['parameters']);
else function_to_call();
};
当然,这是行不通的,因为参数以“parameter1,parameter2”的形式出现在一个字符串中,所以你最终得到
function_to_call("parameter1, parameter2");
而不是
function_to_call(parameter1, parameter2);
有任何想法吗?感谢您的时间!
扩展:
传递给函数的参数代表页面中元素的“id”;所以被调用的函数将尝试通过以下方式获取这些元素:
document.getElementById(parameter1);
...some other things...
document.getElementById(parameter2);