views:

37

answers:

1

i load a form into a jquery ui dialog. i have a submit button (inside my form - NOT the actual dialog buttons) that calls a controller action but i can't figure out how to close the dialog after the submit is called as i dont have any event handler that i am attaching.

is there anyway of doing this besides changing the submit to input type=button?

i know in jquery i can capture the submit

$('#positionForm').submit(function () {
    // do stuff
    return true;
});

but this seems to fire before submitting so i dont want to close the dialog yet.

is there anything wrong with the below code:

$('#positionForm').live('submit', function () {

    $.post('/MyController/Action', $("#positionForm").serialize(), function (data) {
            alert(data);
    }, "html");

    closeModalPopup();
    return false ;
});
+1  A: 

For updated question: You can call the close code in the success callback, like this:

$('#positionForm').live('submit', function () {
  $.post('/MyController/Action', $(this).serialize(), function(data) {
    $('#positionForm').closest(".ui-dialog-content").dialog("close");
  }, "html");
  return false;
});

Original Answer: You can attach a form submit handler, for example:

$("#myform").submit(function() {
  $(this).closest(".ui-dialog-content").dialog("close");
});

You can give it a try here.

Nick Craver
@Nick Craver - see my updated question. will i lose any of the content if i close the dialog before submitting the form as this callback is more of a "beforeSubmit" correct ?
ooo
@ooo - How are you submitting it, some ajax plugin?
Nick Craver
@Nick Craver - no, just regular submit button inside a form tag
ooo
@ooo - I'm not following...in that case the page is going to redirect anyway, and that'll certainly close your dialog...something I'm missing?
Nick Craver
@ooo - Also closing the dialog itself doesn't delete any content, it just hides the dialog, only your code overwriting it or a "destroy" is going to lose content.
Nick Craver
@Nick Craver - ok , i think my questin is a bit unclear. I want to submit the form but not redirect. i simply want to click the submit button, which should then close the dialog and have my initial page viewable in its current state. i am trying to avoid a page reload
ooo
@ooo - In that case, whatever the `success` method is on the ajax call you're making, that's where you'd call this, or use a different selector not related to `this`...I can't say for sure without seeing how you're doing the AJAX submit.
Nick Craver
@Nick Craver - i updated the question with my submit callback code
ooo
@ooo - Updated the answer to match your code :)
Nick Craver