简体   繁体   English

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

[英]Cannot call a user defined JS function after return the AJAX content

I am having a problem of calling the user defined JS function after I make the AJAX call. 在进行AJAX调用后,我遇到了调用用户定义的JS函数的问题。 Basically, I created couple radio buttons on the main html page. 基本上,我在主html页面上创建了几个单选按钮。 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. 当用户点击其中一个单选按钮时,它将触发AJAX调用并返回我在主html页面中设置的“div”内容中的另一个html文件。 The other html file contains a user defined JS function (eg "updateContent()") which use the onclick event handler to call the function. 另一个html文件包含用户定义的JS函数(例如“updateContent()”),它使用onclick事件处理程序来调用该函数。 When I'm running the app, and click on the button. 当我运行应用程序时,单击按钮。 I had seen the firebug was complaining the "updateContent() is not defined" error. 我曾经看到firebug抱怨“updateContent()没有定义”错误。 The function itself works fine and must be defined properly. 函数本身工作正常,必须正确定义。 Please help me about this! 请帮我解决这个问题! Thanks. 谢谢。

The magic Google words here are "JSON-P" and the " On Demand Javascript " pattern. 这里神奇的Google字样是“JSON-P”和“ On Demand Javascript ”模式。 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. 然后javascript被解释,你的功能被定义,钉子被发现,马被穿上,王国不会丢失。

This is because the js code you insert via innerHTML never will be executed. 这是因为你通过innerHTML插入的js代码永远不会被执行。 You might need to extract it from the HTML code run it through eval . 您可能需要从HTML代码中提取它,然后通过eval运行它。 PrototypeJS have a configuration option to do this automatically. PrototypeJS有一个配置选项可以自动执行此操作。

I would recommend you to separate out the js code and include it with the rest of your js code. 我建议你将js代码分开,并将其与其余的js代码包含在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM