views:

232

answers:

3

Hello,

How to close jQuery Dialog within the dialog without using the close button?

Inside the ui dialog is a simply form request and if a successful submission occurs, then the ui dialog automatically closes and refreshes the parent page.

    <script type="text/javascript">
        $(document).ready(function () {
            $("#form-dialog").dialog({
                autoOpen: true,
                modal: true,
                width: 200,
                draggable: true,
                resizable: true
            });
        });
    </script>


    <div id="form-dialog" title="Form Submit">
    <form action="default.aspx" method="post">
    <input type="text" name="name" value=" " />    
    <input type="submit" value="submit" />

    <a href="#" id="btnDone">CLOSE</a>

    <script type="text/javascript">
    $(document).ready(function () {
        $("#btnDone").click(function () {
            $(this).dialog('close');
        });
    });
    </script>

    </form>
    </div>

-imperialx

+1  A: 

you can close it programmatically by calling

$('#form-dialog').dialog('close')

whenever you want.

Jason
But the form tag is inside the ui dialog. No jquery files inside the ui dialog, just a plain form tag submission.How can I use the $('#form-dialog').close() inside the #form-dialog? It's like closing itself after you click something or a waiting timer occurs inside the ui dialog.
imperialx
if you are using jquery to open the dialog, you can use jquery to close it. in the line right under where you declare the dialog, set `$('#mySubmitButton').live('click', function() { mySubmitFunction(); $('#form-dialog').close(); });`
Jason
Yes, but how about when I'm inside the dialog? I think the equivalent of this is something like $(window).unload() where the pop-up form automatically closes itself rather than clicking the jquery-ui-dialog close button.
imperialx
dude. how do you know when the user is done w/your form? at whatever point that is, invoke `$('#form-dialog').close()`
Jason
I have this similar issue but I don't think it's been marked as answer...http://stackoverflow.com/questions/1644365/closing-a-jquery-modal-dialog-from-a-remote-page
imperialx
this will not work....you need $('selector').dialog('close') just .close will not work
redsquare
err... yeah, what @redsquare said
Jason
+1  A: 

You need

$('selector').dialog('close');

redsquare
Hi redsquare, I've added a few lines on my code above, may I know why it won't close the dialog? As you can see, the close button is inside the ui-dialog. Thanks.
imperialx
because in your case this refers to the button not the dialog
redsquare
change $(this) to $("#form-dialog")
redsquare
A: 

@redsquare: I fix that part, but still the ui-dialog didn't close. Looks like the function ...$("#btnDone").click(function ()...didn't find the id="form-dialog" because it is within this dialog.

By the way, how can I reply to individual comments here in stackoverflow? I'm starting to get loss here and so I open up a OpenId account.