IE中通过ajax加载的部分视图中jquery未定义
我有一个包含 JQuery UI 选项卡小部件的网页。选项卡小部件通过 AJAX 加载选项卡。在其中一个选项卡页面(将其命名为 DescriptionPage)中,我有一个将通过 ajaxForm 插件提交的表单。
<div id="tabs">
<ul>
<li>
<a href="DescriptionPage">Description Page</a>
</li>
</ul>
</div>
这是我的DescriptionPage的内容。
<form id="myForm">
<!-- Form elements goes here -->
</form>
<script>
$(function(){
$('#myForm').ajaxForm(function (response) {
$('#myForm').parent().empty().append(response);
});
});
</script>
提交表单后,将返回相同的 DescriptionPage,包括表单和脚本。因此表单内容被服务器端的响应所替换。响应还包含验证消息。
问题是,整个场景在 Chrome 和 Firefox 中运行良好。但在 Internet Explorer 8 中,出现了一个奇怪的问题。
当选项卡首次加载时,JavaScript 会成功执行。当用户提交表单并放置响应时,IE 无法执行我的 javascript,并显示“JQuery 未定义”。
为什么 IE 在通过 ajax 加载的内容中调用 JQuery 失败? 有解决方法吗?
PS:我想将脚本与 html 分开,但这根本不是一个选项:(
P.S2:由于愚蠢的 IE,我的 javascript 和 CSS 文件变得一团糟。
I have a web page consisting a JQuery UI Tabs widget. Tab widget loads the tabs via AJAX. In one of the tab pages (name it DescriptionPage), I have a form which will be submitted via ajaxForm plugin.
<div id="tabs">
<ul>
<li>
<a href="DescriptionPage">Description Page</a>
</li>
</ul>
</div>
This is content of my DescriptionPage.
<form id="myForm">
<!-- Form elements goes here -->
</form>
<script>
$(function(){
$('#myForm').ajaxForm(function (response) {
$('#myForm').parent().empty().append(response);
});
});
</script>
After form is submitted, the same DescriptionPage is returned, both the form and script. So the form content is replaced with the response of the server side. The response also contains validation messages.
The problem is, The whole scenario works well in Chrome and Firefox. But in Internet Explorer 8, a strange issue happens.
When the tab is first loaded, the javascript is successfully executed. When user submits the form and the response is put, IE fails to execute my javascript, saying "JQuery is not defined".
Why IE fails to call JQuery inside the content loaded via ajax?
Is there a workaround?
P.S: I thought seperating the script from html, but it is not an option at all :(
P.S2: My javascript and CSS files became a mess because of stupid IE.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这似乎在 Internet Explorer 6 中对我有用:
index.html:
DescriptionPage.html:
This seems to work for me in Internet Explorer 6:
index.html:
DescriptionPage.html:
请参阅:jQuery 1.6.1、IE9 和 SCRIPT5009:' $' 未定义
用户 ID“Black”为我提供了正确的方向。
我是通过在对话框上使用 .AjaxForm 上传的。
之后,我用另一个 AjaxForm 替换该 Form。
然后我遇到了“$未定义”或“jQuery未定义”的错误
主要原因是我对第二个对话框应用了“render :layout => false”。这就是为什么所有 js 库(包括 jQuery )都没有正确加载。
所以,对于第二次对话的部分内容,我必须补充一下。
Please refer to : jQuery 1.6.1 , IE9 and SCRIPT5009: '$' is undefined
User id "Black" has provided the right direction for me.
I was uploading by using .AjaxForm on a dialog.
after that, I replace that Form, with another AjaxForm.
Then I had a bug with "$ is undefined", or "jQuery is undefined"
The main reason is I applied "render :layout => false" for the second Dialog. That's why all the js library (include jQuery ) have not been loaded properly.
So that, on the partial of the Second Dialog, I have to add.
尝试这样的操作,看看是否可以解决问题:
应该做的是将现有表单的内容替换为响应中返回的元素的内容。而不是替换整个表单元素并再次执行JS。它可能会解决您的问题。
Try something like this to see if it resolves the problem:
What this should do is replace the contents of the existing form with the contents of the element returned in the response. Instead of replacing the entire form element and executing JS again. It may work around your problem.
你可以尝试这样的事情:
用这种方法你可以将 $ 封装在函数中,并且它不会被其他 js 框架弄乱(如果你使用任何框架)。我不知道这是否适用于 IE,我无法测试它,但希望它会:)
You could try something like this:
with that approach you encapsulate the $ within the function and it can't be messed up from other js frameworks (should you use any). I don't know if that will work with IE and I can't test it but hopefully it will :)
如果您使用 IE,请确保您使用的是 jQuery 版本 1。IE 不支持 jQuery 版本 2....即使地球上的其他所有内容都支持版本 2....
http://jquery.com/download/
If you are using IE, make sure you are using jQuery version 1. jQuery version 2 is not supported by IE....even though everything else on the planet supports version 2....
http://jquery.com/download/