jquery-dialog 无法使用 jquery-functions

发布于 2024-10-19 22:58:53 字数 1441 浏览 0 评论 0原文

我将一些来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-10-26 22:58:53

您包装函数的 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 whole script tag at the end of the markup you're sending from your controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文