jQuery 打开 jQueryUI 对话框后如何执行函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5144338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:37:56  来源:igfitidea点击:

How to execute function after opening a jQueryUI Dialog?

jqueryjquery-uidialog

提问by grunk

On my web page i have some links like :

在我的网页上,我有一些链接,例如:

<div id="toolbarButtons">
    <a href="actualites/addLink" id="liens" rel="lien" title="Insérer un lien" class="toolbarButton"><span><img src="pub/struct/picto/icon_toolbar-link.gif" alt="Liens" />Lien</span></a>
    <a href="actualites/addImage" rel="image" title="Insérer une image" id="img" class="toolbarButton"><span><img src="pub/struct/picto/icon_toolbar-img.gif" alt="Liens" /> Image(s)</span></a>
</div>
<div id="dialogbox"></div>

First of all i init my dialogbox by calling :

首先,我通过调用来初始化我的对话框:

initDialog : function() {
    $('#dialogbox').dialog({
        bgiframe:true,
        autoOpen:false,
        width:500,
        modal:true
    });
}

then i attach the the dialog on click event :

然后我附上点击事件的对话框:

$('.toolbarButton').click(function(e){
            e.preventDefault();
            actu.dialogManager($(this));
});

dialogManager : function(elem) {

    elem.blur();
    var title   = elem.attr('title');
    var href    = elem.attr('href');
    var rel     = elem.attr('rel');

    $('#dialogbox').dialog('option','title',title);

    if(rel == 'lien')
    {
         $('#dialogbox').dialog('option','buttons',{
            'Add' : function(){
                            //TODO
            },
            'Cancel' : function(){
                $('#linkText').val('');
                $('#linkUrl').val('');
                $(this).dialog('close');
            }
        });

        $('#dialogbox').load(href).dialog('open');

    }
}

As you can see , the content of the dialog box is fetched with ajax. The dialog contain some input. I have a last function which is supposed to edit the content of the input but i don't know how and where to call it. It need to be called after the opening of the dialog to be efficient. How can i do that ?

可以看到,对话框的内容是通过ajax获取的。该对话框包含一些输入。我有一个应该编辑输入内容的最后一个函数,但我不知道如何以及在哪里调用它。需要在打开对话框后调用才能有效。我怎样才能做到这一点 ?

doing it juste after

做正义之后

$('#dialogbox').load(href).dialog('open');

won't work because of the asynchronous loading (called before the dialog is fully loaded).

由于异步加载(在对话框完全加载之前调用)而无法工作。

Thanks for your help.

谢谢你的帮助。

回答by nate_weldon

You could do something when the dialog is opened.

当对话框打开时你可以做一些事情。

initDialog : function() {
  $('#dialogbox').dialog({
      bgiframe:true,
      autoOpen:false,
      open: function() {
          // do something on load
       },
      width:500,
      modal:true
  });
}

回答by JeffB

Can you do something like this?

你能做这样的事情吗?

$('#dialogbox').load(href, function() {
   //your edit function?
   $(this).dialog('open');
   });

That function will be called when the load is completed.

加载完成后将调用该函数。