Javascript:用字符串替换 onChange 事件

发布于 2024-12-09 06:59:44 字数 511 浏览 2 评论 0原文

我目前正在 sharepoint 2007 的范围内工作,并且有一些代码可以将下拉列表变成组合框。我所拥有的工作得很好,但是此代码是供业务分析师和内容创建者使用的,因此它必须尽可能简单。所以我将它压缩成一个函数,除了 onChange 事件之外,一切都进展顺利。

我已设法提取原始的 onChange 事件,但由于我的下拉列表,我必须替换参数。因此,我将其转换为字符串,替换参数,然后需要将其重新转换为函数:

var onChangeFunction = "function (){alert("your function has been called")}"    

//The function 'attachEvent' is not common javascript. It is a custom function
//that works for my combobox.
combobox.attachEvent("onChange",(function)onChangeFunction);

这可能吗?

I am currently working within the confines of sharepoint 2007 and have some code that turns a dropdownlist into a combobox. What i have works perfectly fine, however this code is meant to be used by business analysts and content creators so it has to be as simple as possible. So i am condensing it into a function and all has gone swimmingly except for the onChange event.

I have managed to extract the original onChange event, though due to my dropdownlist i have to replace a parameter. So i convert it to a string, replace the parameter and need to reconvert it to a function a la:

var onChangeFunction = "function (){alert("your function has been called")}"    

//The function 'attachEvent' is not common javascript. It is a custom function
//that works for my combobox.
combobox.attachEvent("onChange",(function)onChangeFunction);

Is that even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无风消散 2024-12-16 06:59:44

您可以使用new Function,但我认为您正处于滑坡状态。

var fn = new Function("{alert(\"your function has been called\")}")
combobox.attachEvent("onChange",fn);

You can use new Function but I think you are on a slippery slope playing with this.

var fn = new Function("{alert(\"your function has been called\")}")
combobox.attachEvent("onChange",fn);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文