简单模式ajax调用

发布于 2024-07-30 19:22:25 字数 564 浏览 1 评论 0原文

我正在使用以下代码在我的应用程序中实现 Simplemodal:(

$(".dialog-link").live('click', function(e) {
 e.preventDefault();
 $.get($(this).attr('href'),function(data) {
  $.modal(data, {onOpen: open, position: ['10%','30%']});
 );
});

仅供参考:onOpen 回调仅设置一些高度)

ajax 调用返回的文档(包含在数据中)有一些 jquery 调用 datepicker 等。但是当我的对话框显示 日期选择器将无法工作。

我知道我可以从 onShow 回调中打开日期选择器,但理想的情况是调用 data 中包含的函数,因为每个对话框可以有不同的 jquery 调用。

有没有办法做,例如

onShow: function(dialog) { dialog.data.my_function(); }

谢谢, 迈克尔

I'm implementing Simplemodal in an application of mine using this code:

$(".dialog-link").live('click', function(e) {
 e.preventDefault();
 $.get($(this).attr('href'),function(data) {
  $.modal(data, {onOpen: open, position: ['10%','30%']});
 );
});

(FYI: the onOpen callback just sets some height)

The document returned by the ajax call (contained in data) has some
jquery calls to datepicker, etc. But when my dialog displays the
datepicker won't work.

I know I can open the datepicker from the onShow callback, but the ideal would be to call a function contained in data because each dialog can have different jquery calls.

Is there a way to do for example

onShow: function(dialog) { dialog.data.my_function(); }

Thanks,
Michael

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

鹤舞 2024-08-06 19:22:27

扩展@And 答案。 由于 live() 已在 jQuery 1.9 中删除:

$(document).on("click","a.dialog-link",function(e) {
    e.preventDefault();
    $.get($(this).attr("href"),function(data) {
        $.modal(data, {position: ["10%','30%"], onShow: function(dialog){
            external_function()
        }});
    });
});

Expanding on @And answer. Since live() has been removed in jQuery 1.9:

$(document).on("click","a.dialog-link",function(e) {
    e.preventDefault();
    $.get($(this).attr("href"),function(data) {
        $.modal(data, {position: ["10%','30%"], onShow: function(dialog){
            external_function()
        }});
    });
});
っ左 2024-08-06 19:22:27

正如 DOM 检查可能揭示的那样(firebug 总是很方便......),模式对话框将外部文档加载到 div 中,去掉 < /代码> 标签。 它似乎还导入到加载文档中定义的主窗口对象和函数的范围中。

因此,像在另一个窗口或框架的范围内一样调用函数(dialog.data.my_function)将不起作用。

对我有用的是将外部函数直接绑定到 onShow 事件。

主文档:

    <script type="text/javascript">
        $("a.dialog-link").live('click', function(e) {
           e.preventDefault(); 
           $.get($(this).attr('href'),function(data) {
           $.modal(data, {position: ['10%','30%'], onShow: function(dialog){ 
                                                           external_function()
           }});
         });
    </script>

外部文档(加载到模式框中:)

<html><head><title>bla bla </title>
   <script type="text/javascript">
      function external_function(){$("#external_content").text("UPDATED!")};
   </script>
</head>
<body>
 <div id="external_content"> .... </div>
</body>
</html>

希望这有帮助:)

As a DOM inspection may reveal (firebug is always handy...), the modal dialog loads the external document into a div, stripping out the <html> and <head> tags. It also seem to import into the scope of the main window objects and functions defined in the loaded document.

Therefore calling a function as if it was in the scope of another window or frame ( dialog.data.my_function ) will not work.

What works for me instead is to bind directly the external function to the onShow event.

main document:

    <script type="text/javascript">
        $("a.dialog-link").live('click', function(e) {
           e.preventDefault(); 
           $.get($(this).attr('href'),function(data) {
           $.modal(data, {position: ['10%','30%'], onShow: function(dialog){ 
                                                           external_function()
           }});
         });
    </script>

External document (loaded into the modal box:)

<html><head><title>bla bla </title>
   <script type="text/javascript">
      function external_function(){$("#external_content").text("UPDATED!")};
   </script>
</head>
<body>
 <div id="external_content"> .... </div>
</body>
</html>

Hope this helps :)

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