jquery-dialog 无法使用 jquery-functions
我将一些来自 get-request 的 html 传递给我的 Jquery-dialog 函数。
然而,在对话框内,我无法再使用任何 jquery 函数,尽管我 在调用对话框的页面中包含 jquery。
如果我再次将其包含在对话框的 html 中,对话框的“确定”按钮将停止工作,并且在关闭对话框后,我调用该对话框的页面上的所有其他内容也不再工作。
我希望在调用对话的页面中包含 jquery 就足以让对话访问 jquery,但情况似乎并非如此。
这是我的代码:
dialog.js:
var dialogDiv = $(document.createElement('div'));
function myDialogue(content) {
dialogDiv.html(content);
dialogDiv.dialog({autoOpen: false, modal: true, buttons: { "ok": function() {
$(this).dialog("close");
$(this).dialog("destroy").remove();
} } });
dialogDiv.dialog('open');
dialogDiv.scrollTop(0);
return true;
}
调用 myDialogue =>
main.html:
<script type="text/javascript" src="/path/to/jquery.js"></script>
<script type="text/javascript" src="/path/to/dialog.js"></script>
$.get("/path/to/controller/index", {param: "value"}, function(content) {
myDialogue(content);
});
从我的控制器返回的 html,它被传递到对话框:
<script type="text/javascript">
$(document).ready(function() {
$("#edit_div :checkbox").click(function () {
// this NEVER gets called unless I include jquery again, but
// then the ok button and the rest of the page do not work anymore.
alert("checked!!!");
});
});
</script>
<div id="edit_div">
<input type="checkbox" value="mycheck" name="mycheck"/>
....
I pass some html coming from a get-request to my Jquery-dialog function.
Inside the dialog however I cannot make use of any jquery-functions anymore although I
have included jquery in the page calling the dialog.
If I include it AGAIN in the dialog's html the dialog's "ok"-button stops to work and all other stuff on the page from which I have called the dialog also does not work anymore after having closed the dialog.
I would expect that having included jquery in the page which calls the dialogue would suffice for the dialogue to have access to jquery but this does not seem to be the case.
Here's the code I have:
dialog.js :
var dialogDiv = $(document.createElement('div'));
function myDialogue(content) {
dialogDiv.html(content);
dialogDiv.dialog({autoOpen: false, modal: true, buttons: { "ok": function() {
$(this).dialog("close");
$(this).dialog("destroy").remove();
} } });
dialogDiv.dialog('open');
dialogDiv.scrollTop(0);
return true;
}
call to myDialogue =>
main.html:
<script type="text/javascript" src="/path/to/jquery.js"></script>
<script type="text/javascript" src="/path/to/dialog.js"></script>
$.get("/path/to/controller/index", {param: "value"}, function(content) {
myDialogue(content);
});
the html returned from my controller which is passed to the dialog:
<script type="text/javascript">
$(document).ready(function() {
$("#edit_div :checkbox").click(function () {
// this NEVER gets called unless I include jquery again, but
// then the ok button and the rest of the page do not work anymore.
alert("checked!!!");
});
});
</script>
<div id="edit_div">
<input type="checkbox" value="mycheck" name="mycheck"/>
....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您包装函数的
document.ready
事件仅在页面首次加载时才会被调用。您可以尝试从
document.ready
包装器中取出该函数,并将整个script
标记放在从控制器发送的标记的末尾。The
document.ready
event which you've wrapped your function in only gets called when the page is first loaded.You could try taking the function out of the
document.ready
wrapper and placing the wholescript
tag at the end of the markup you're sending from your controller.