使用 jQuery 的 .load() 函数后 ('#').dialog is not a function 错误
我有以下代码:
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'mydialog',
// additional javascript options for the dialog plugin
'options'=>array(
'title'=>'Confirmar',
'resizable'=>'false',
'autoOpen'=>false,
'modal'=>true,
'buttons'=>array('Eliminar'=>'js:function(){deleteMessage();$(this).dialog("close");}',
'Cancelar'=>'js:function(){$(this).dialog("close");}',),
),
));?>
<div style="display:none">Do you confirm you want to delete the item?</div>
<?php
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
<input type="button" onclick="js:openDlg()" value="Open the dialog">
<script language="javascript" type="text/javascript">
function openDlg(){
$("#mydialog").dialog("open");
}
</script>
这工作得很好,直到我需要调用 jQuery 的 .load() 函数。出于测试目的,我有一个调用 .load() 方法的按钮(尽管应该在文档准备好时调用它)。如果我在单击此试用按钮之前点击“打开对话框”按钮,则对话框将正确打开。否则,它会失败并出现以下错误:
$("#mydialog").dialog 不是函数 $("#mydialog").dialog("打开");
试用按钮:
<input type="button" onclick="js:load_wall()" value="Load Messages">
function load_wall(){
var liga = $("#liga_id").val();
$('#div_wall_messages').load('displayMessages',{liga_id: liga}, function(){
});
请,非常欢迎任何帮助。这个错误让我发疯。谢谢你!!
I have the following code:
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'mydialog',
// additional javascript options for the dialog plugin
'options'=>array(
'title'=>'Confirmar',
'resizable'=>'false',
'autoOpen'=>false,
'modal'=>true,
'buttons'=>array('Eliminar'=>'js:function(){deleteMessage();$(this).dialog("close");}',
'Cancelar'=>'js:function(){$(this).dialog("close");}',),
),
));?>
<div style="display:none">Do you confirm you want to delete the item?</div>
<?php
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
<input type="button" onclick="js:openDlg()" value="Open the dialog">
<script language="javascript" type="text/javascript">
function openDlg(){
$("#mydialog").dialog("open");
}
</script>
This works PERFECTLY, until i needed to call jQuery's .load() function. For testing purposes, i have a button which calls the .load() method (although supposedly it should be called when the doc is ready). If i hit the open dialog button before clicking this trial button, the dialog opens correctly. Else, it fails with the following error:
$("#mydialog").dialog is not a function
$("#mydialog").dialog("open");
Trial button:
<input type="button" onclick="js:load_wall()" value="Load Messages">
function load_wall(){
var liga = $("#liga_id").val();
$('#div_wall_messages').load('displayMessages',{liga_id: liga}, function(){
});
Please, any help is more than welcome. This error is driving me crazy. thank you!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
什么是
displayMessages
?它是一个包含脚本的页面吗?该页面是否包含 jQuery 的另一个副本?如果您包含 jQuery 的第二个副本,您将覆盖现有副本 — 可能您正在使用未加载 Dialog 插件的新插件覆盖已加载 Dialog 插件的 jQuery。如果您想加载到 div 中,并且正在加载的文件中包含一整页 HTML(包括
、
s et al),那么您应该只将您想要的内容片段(通过
id
)加载到目标中。在所有情况下,请避免load()
包含的 HTML 内容;根据具体情况,结果要么没有任何结果,要么毫无意义。
另外,上面所有代码中的
js:
不执行任何操作,应省略。What's
displayMessages
? Is it a page with scripts in it? Is it a page that includes another copy of jQuery? If you include a second copy of jQuery you will be overwriting your existing copy—possibly you are overwriting a jQuery that has the Dialog plugin loaded into it with a new one that doesn't.If you want to load into a div and what you've got in the file being loaded is a full page of HTML (including
<head>
,<script>
s et al), then you should only load the fragment of content (byid
) that you want into your target. In all cases avoidload()
ing HTML content that contains<script>
; the results are, depending on circumstance, either nothing, or nothing sensible.Also
js:
in all your code above does nothing and should be omitted.如果加载是问题所在,则您可能已加载(并覆盖)当前加载的库,其中可能包括带有对话框插件的 jQuery。确保加载一个干净的页面,(每当我 AJAX 时,我通常只生成一个
或
,无论我需要什么,而不是 检查浏览器的网络/资源,
如果失败,请确保包含正确的 javascript 文件,其中包括 jQuery 和看似 jQuery UI,
跟踪这些脚本文件的情况,并查看是否有任何其他错误消息。 (也许 404 未找到?)在 javascript 文件上。
If the load is the problem, it's likely that you've loaded (and overwritten) the currently loaded libraries, which might include jQuery with your dialog plugin. Make sure you load a clean page, (Whenever I AJAX, I generally only generate a single
<div>
or a<ul>
, whatever I need, and not the entire webpage.If that fails, make sure the proper javascript files are included, that include jQuery and seemingly also jQuery UI.
Check in your browser's network/resources tracking what's up with those script files, and see if there are any other error messages (maybe a 404 not found?) on the javascript files.
正如上面已经解释的,您可能会覆盖这些库。
处理它的一种方法是确保加载正确的文件。
另一种方法是在对话框和对话框中的内容之间插入 iframe。浏览器将 iframe 视为具有自己的脚本的单独页面。因此,iframe“之上”的内容脚本将与 iframe“之下”的对话框脚本分开。
由于您使用的是 Yii,请检查此 wiki。
As already explained above, you probably override the libraries.
One way of dealing with it is to ensure that the correct files are loaded.
Another way is to insert an iframe between the dialog and the content in the dialog. Iframes are treated by the browser as a separate page with its own scripts. So the scripts of the content "on top of" the iframe, will be separate from the scripts of the dialog "under" the iframe.
Since you are using Yii, check this wiki.