Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jquery ui dialog to confirm action for form submission

Tags:

jquery

confirm

I have multiple forms on a page, for each of them I want the user to confirm before form submission. but when the user confirms to submit, how do I let this dialog know which form the user is sumbitting? Does it take custom parameters? Thanks.

$("#dialog-confirm").dialog({
        resizable: false,
        height:140,
        modal: true,
        buttons: {
            'Confirm submit': function() {
                document.______???????_____.submit();
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        }
    });
$('.allForms').submit(function(){
      $('#dialog-confirm').dialog('open');
});
like image 891
redbluegreen Avatar asked Mar 10 '10 21:03

redbluegreen


3 Answers

Based on Nick Craver his answer, you can write it this way:

$('.allForms').submit(function(){
      currentForm = this;

      $('#dialog-confirm').dialog({
        resizable: false,
        height:140,
        modal: true,
        buttons: {
            'Confirm submit': function() {
                currentForm.submit();
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        }
      });
      return false;
});
like image 116
Daan Avatar answered Oct 16 '22 02:10

Daan


You can store it in a variable like this:

var currentForm;
$("#dialog-confirm").dialog({
        resizable: false,
        height:140,
        modal: true,
        buttons: {
            'Confirm submit': function() {
                currentForm.submit();
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        }
    });
$('.allForms').submit(function(){
      currentForm = this;
      $('#dialog-confirm').dialog('open');
      return false;
});

Since you're just using this and immediately just leaving the page, no real reason to make it any more complicated than that.

like image 17
Nick Craver Avatar answered Nov 19 '22 01:11

Nick Craver


Or how about

$(this.form).submit();
like image 1
Eric Avatar answered Nov 19 '22 01:11

Eric