简体   繁体   English

一次关闭一个jquery-ui对话框

[英]closing a jquery-ui dialog one at a time

I'm having an issue with multiple jQuery UI dialogs. 我在使用多个jQuery UI对话框时遇到问题。 What I want is that I can have multiple dialogs, and a click on a button with the class ".close-modal" will only close the dialog in which that button was located. 我想要的是我可以有多个对话框,并且单击类为“ .close-modal”的按钮只会关闭该按钮所在的对话框。 Now, first for a little background: I have a simple function in the "main" parent of the site, which will open a modal dialog by clicking on a link with the class ".modal"; 现在,首先介绍一下背景:我在站点的“主要”父级中有一个简单的函数,它将通过单击类为“ .modal”的链接打开一个模式对话框。

In the parent 在父母中

function modal( href, title ) {
    var $dialog = $('<div></div>');
    $dialog.html('<iframe class="modal" name="' + title + '" style="border: 0px;" src="' + href + '" width="100%" height="100%"></iframe>');
    $dialog.dialog({
        autoOpen: false,
        modal: true,
        height: 650,
        width: 950,
        title: title
    });


    $dialog.dialog( 'open' );

    $(document).bind( 'close-dialog', function( ) {
        $dialog.dialog( 'close' );
    });
}

$('a.modal').click( function( event ) {
    event.preventDefault( );
    modal( $(this).attr( 'href' ), $(this).attr( 'title' ) );
}

This code works the way I want it to. 这段代码按照我想要的方式工作。 To open a dialog from a dialog, I call the parent window's modal function with the correct parameters, so that multiple dialogs can be displayed within the parent: 要从对话框中打开对话框,请使用正确的参数调用父窗口的模态函数,以便可以在父窗口中显示多个对话框:

In the dialog 在对话框中

( function( $ ) {
    $(document).ready( function( ) {
        $('a.modal').click( function( event ) {
            event.preventDefault( );
            parent.modal( $(this).attr( 'href' ), $(this).attr( 'title' ) );
        }

        /** Trigger the close-dialog event on the parent to close the current dialog. */
        $('.close-dialog').click( function( ) {
            parent.jQuery( parent.document ).trigger( 'close-dialog' );
        });
    });
})( jQuery );

So, in essence; 因此,本质上; I've defined an event on the parent window ("close-dialog"), which can be called by the code within the iframe which will call $dialog.dialog( 'close' ); 我已经在父窗口上定义了一个事件(“ close-dialog”),该事件可以由iframe中的代码调用,该代码将调用$ dialog.dialog('close');。 This works brilliantly; 这非常出色; I can create multiple dialogs, even from within dialogs, but the issue is that once I click a button.close-dialog , it closes all of my dialogs. 我甚至可以在对话框中创建多个对话框,但是问题是,一旦单击button.close-dialog ,它就会关闭所有对话框。

Is there any way to determine from which dialog the event was called? 有什么方法可以确定从哪个对话框中调用了该事件? Or bind the event to the current dialog? 还是将事件绑定到当前对话框?

You could use jquery modal's buttons option. 您可以使用jquery modal的按钮选项。

<script>
$(function() {
    $( ".myModals" ).dialog({
        modal: true,
        buttons: {
            Close: function() {
                $( this ).dialog( "close" );
            }
        }
    });
});
</script>

This should do it for all your modals in one go, and the buttons it creates only apply to themselves. 这应该一次性完成所有模态的操作,并且它创建的按钮仅适用于自己。 Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM