如何在 JavaScript 中计算完整的函数字符串(包括参数)
我有一个选择(下拉列表),其中包含与操作相对应的选项。每个操作对应于一个通用 JavaScript 函数,应在选择的更改事件上调用该函数。所以它看起来像这样:
<select id="id-select-action" class="select-action right" value="Actions">
<option class="select-title">Actions</option>
<optgroup label="General Actions">
<option value="load_new_page('/url/to/new/page')">create a foo</option>
<option value="load_cbox('/url/to/cbox/contents')">manage tags</option>
</optgroup>
</select>
我的服务器代码动态生成每个选项的值字符串(这是对 javascript 函数的调用,具体取决于操作的作用和显示方式),并且服务器还动态生成参数(视图的 url) )。
我在选择的更改事件上有一个 jquery 侦听器,我想在选项的值字符串中调用 javascript 函数。
$("#id-select-action").change(function() {
function_call = $("#id-select-action option:selected").val();
//How do I execute the function_call?
});
似乎每个人都讨厌 eval() 。 。 。但这是一个合适的案例吗,还是我应该做点别的事情?我看了这个如何将字符串转换为javascript函数调用? . 。 。但在其中,您只需处理名称,而不是完整的函数调用字符串,包括参数。
我这样做的原因是因为我正在为应用程序的某些类型的视图创建一个通用框架,并且每个页面的操作可能不同(并且具有不同的 url 和/或调用不同的 javascript 函数) 。
谢谢!
I have a select (dropdown list) with options which correspond to actions. Each action corresponds to a generic javascript function that should be called on the changed event for the select. So it looks like this:
<select id="id-select-action" class="select-action right" value="Actions">
<option class="select-title">Actions</option>
<optgroup label="General Actions">
<option value="load_new_page('/url/to/new/page')">create a foo</option>
<option value="load_cbox('/url/to/cbox/contents')">manage tags</option>
</optgroup>
</select>
My server code is dynamically generating each option's value strings (which are calls to javascript functions, depending on what the action does and how it is displayed) and the server also dynamically generates the parameters (the urls to the views).
I have a jquery listener on the change event for the select, which I want to call the javascript function in the value string in the option.
$("#id-select-action").change(function() {
function_call = $("#id-select-action option:selected").val();
//How do I execute the function_call?
});
It seems everyone hates eval() . . . but is this a proper case for it, or should i be doing something else? I looked at this How to turn a String into a javascript function call? . . . but in that one, you only have the name to deal with, not the full function call string, including the parameters.
The reason I'm doing it this way is because I'm creating a general framework for certain types of views of our application, and the actions may be different (and have different url's and/or call a different javascript function) for each page.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为评估会有所帮助......在这种情况下。
http://www.w3schools.com/jsref/jsref_eval.asp
但请小心关于使用 eval 并检查条件是否适合您的情况。
I think eval will help.. in the condition.
http://www.w3schools.com/jsref/jsref_eval.asp
But please be careful about using eval and check conditions if its fits in your case.
使用此 fn 从字符串创建 fn
//现在在更改事件中使用上面的 fn
http://jsfiddle.net /praveen_prasad/fk8fh/
Use this fn to create a fn from string
//Now use above fn in your change event
http://jsfiddle.net/praveen_prasad/fk8fh/