返回AJAX内容后无法调用用户定义的JS函数

时间:2021-10-10 23:56:41

I am having a problem of calling the user defined JS function after I make the AJAX call. Basically, I created couple radio buttons on the main html page. When the user clicks on one of the radio button, it will trigger the AJAX call and return another html file in the "div" content that I set in the main html page. The other html file contains a user defined JS function (e.g. "updateContent()") which use the onclick event handler to call the function. When I'm running the app, and click on the button. I had seen the firebug was complaining the "updateContent() is not defined" error. The function itself works fine and must be defined properly. Please help me about this! Thanks.

我在进行AJAX调用后遇到调用用户定义的JS函数的问题。基本上,我在主html页面上创建了几个单选按钮。当用户点击其中一个单选按钮时,它将触发AJAX调用并返回我在主html页面中设置的“div”内容中的另一个html文件。另一个html文件包含用户定义的JS函数(例如“updateContent()”),它使用onclick事件处理程序来调用该函数。当我运行应用程序时,单击按钮。我曾经看到firebug抱怨“updateContent()没有定义”错误。函数本身工作正常,必须正确定义。请帮我解决这个问题!谢谢。

2 个解决方案

#1


2  

The magic Google words here are "JSON-P" and the "On Demand Javascript" pattern. Basically, instead of loading a chunk, you create and load a tag. Then the javascript gets interpreted, your function is defined, the nail is found, the horse is shod and the kingdom is not lost.

这里神奇的Google字样是“JSON-P”和“On Demand Javascript”模式。基本上,您可以创建并加载标记,而不是加载块。然后javascript被解释,你的功能被定义,钉子被发现,马被穿上鞋子并且王国不会丢失。

#2


1  

This is because the js code you insert via innerHTML never will be executed. You might need to extract it from the HTML code run it through eval. PrototypeJS have a configuration option to do this automatically.

这是因为你通过innerHTML插入的js代码永远不会被执行。您可能需要从HTML代码中提取它,然后通过eval运行它。 PrototypeJS有一个配置选项可以自动执行此操作。

I would recommend you to separate out the js code and include it with the rest of your js code.

我建议你将js代码分开并将其与其余的js代码包含在一起。

#1


2  

The magic Google words here are "JSON-P" and the "On Demand Javascript" pattern. Basically, instead of loading a chunk, you create and load a tag. Then the javascript gets interpreted, your function is defined, the nail is found, the horse is shod and the kingdom is not lost.

这里神奇的Google字样是“JSON-P”和“On Demand Javascript”模式。基本上,您可以创建并加载标记,而不是加载块。然后javascript被解释,你的功能被定义,钉子被发现,马被穿上鞋子并且王国不会丢失。

#2


1  

This is because the js code you insert via innerHTML never will be executed. You might need to extract it from the HTML code run it through eval. PrototypeJS have a configuration option to do this automatically.

这是因为你通过innerHTML插入的js代码永远不会被执行。您可能需要从HTML代码中提取它,然后通过eval运行它。 PrototypeJS有一个配置选项可以自动执行此操作。

I would recommend you to separate out the js code and include it with the rest of your js code.

我建议你将js代码分开并将其与其余的js代码包含在一起。