tags:

views:

53

answers:

2

I'm trying to do a confirm dialog using jquery, but the form doesn't get submitted at all, this is what I got:

    <script type="text/javascript">
    var currentForm;
    $(function() {
        $("#dialog-confirm").dialog({
            resizable: false,
            height: 140,
            modal: true,
            autoOpen: false,
            buttons: {
                'Delete all items': function() {
                    currentForm.submit();
                    $(this).dialog('close');
                },
                Cancel: function() {
                    $(this).dialog('close');
                }
            }
        });
    });

    function ask() {
        currentForm = $(this).closest('form');
        $("#dialog-confirm").dialog('open');
    }
</script>
<form ... >
    <input type="submit" value="delete" onclick="ask();return false;" />
</form>
+1  A: 

If you're using jQuery, then do it properly and avoid inline Javascript:

$(function (){
  $("form").submit(function (){
    // return false if you don't want this to be submitted
    // maybe do a
    // return ask();
    // but with the code provided it doesn't seem
    // to work like that - ask() is not returning anything at all!
  });
});
Seb
+2  A: 

You need to have your dialog button submit the <form>, like this:

               'Delete all items': function() {
                  $(this).dialog('close');
                  $("#myForm").submit();
                }

The rest of your code is correct, but it's currently just closing the dialog and returning, you need to actually submit the form. Also, it's better to do this as a click handler, instead of onclick, like this (updated for comments):

    var currentForm;
    $(function() {
        $("#dialog-confirm").dialog({
            resizable: false,
            height: 140,
            modal: true,
            autoOpen: false,
            buttons: {
                'Delete all items': function() {
                    $(this).dialog('close');
                    currentForm.submit();
                },
                'Cancel': function() {
                    $(this).dialog('close');
                }
            }
        });
        $(".delete").click(function() {
          currentForm = $(this).closest('form');
          $("#dialog-confirm").dialog('open');
          return false;
        });
    });

Then just give your <input> an class of delete, like this:

<input class="delete" type="submit" value="delete" />
Nick Craver
the problem is that i have lots of forms, each one with a delete button inside of it, it's just a hidden input `<input type="hidden" name='id' value='the id to be delted' />` that makes the difference between these forms
Omu
I could give the forms different names/ids, the name could contain form+theId
Omu
@Omu - You can store a "currentForm = `$(this).closest('form')` in the click function when it opens, then just call `currentForm.submit()` in the delete button's handler, no IDs needed...and just change the delete click handler to use a class or attribute selector.
Nick Craver
@Nick Craver I tried your approach (also updated the question), the form doesn't get submitted
Omu
@Omu - I meant with the answer approach :) What you have won't work overall. The answer is updated to hopefully show exactly what I mean.
Nick Craver
it works with a return false on the input, otherwise it's insta-submit
Omu
Omu - Make sure you have `return false` in the click handler like the answer does!
Nick Craver
:) ya it does, I've added it in the answer
Omu
@Omu - Doh, my fault, didn't even see the edit, thanks for improving this for future googlers :)
Nick Craver