0

I answered my own question because I was an idiot. :)


I've bound a function with jQuery submit() to a form that prevents the default behaviour (with event.preventDefault() and return false) so that it can perform the client-side validation. The script doesn't actually do any form submission (it leaves that to another script).

Now I want to remove the event and revert to the default form behaviour. I need to actually post the data (without AJAX) to another page/website, but I still want to validate it using the script I wrote.

I have considered duplicating the form HTML and submitting it that way, but that would be a last resort.

I have tried using unbind('submit') however that did nothing. I'm guessing it actually unbinds the default behaviour too.

What can I do? Is the default behaviour still bound to the form, just not in use?

Note: I can't modify the validation script nor can I unbind that specific function that prevents the default behaviour.

Source-code available on request, however most of the code is irrelevant, so I will only post snippets (shown below).

function _preventDefaults() {
    //Prevent form from submitting (until we validate it)...
    $('#myform').submit(function(event) {
        event.preventDefault();
        return false;
    });
}
Jared
  • 2,978
  • 4
  • 26
  • 45

3 Answers3

1

You can bypass the bound event using the native method.

$("#myform").get().submit();

Alternatively, you can create a new form and copy the inputs from the old one into it.

$("<form></form").attr("action","somepage.php").append($("#myform :input")).submit();

Since you're letting it redirect, it shouldn't matter that they aren't in the old form anymore.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
0

I believe you want:

_Form.unbind('submit');

If you're using a newer version of jQuery you can also do:

_Form.off('submit');
Bill Criswell
  • 32,161
  • 7
  • 75
  • 66
  • I tried unbinding `submit` but nothing changed. I could be doing it wrong, or doing that will actually unbind all `submit` events (including the default). _Updated question mentioning I've tried this_. – Jared Jul 19 '13 at 02:50
  • 2
    You can't remove the default form behavior (if I'm following you right). I set up a little demo. If you don't calll $('#myForm').unbind('submit') it just logs and sits there. http://jsbin.com/ediyel/2/edit – Bill Criswell Jul 19 '13 at 02:59
  • Thanks for the jsbin! I'll give it a shot on my end. – Jared Jul 19 '13 at 03:07
0

I'm an idiot.

$('#myform').find('INPUT,TEXTAREA,BUTTON').attr('disabled', 'yes');

I wanted to grey out the fields and prevent the user from changing the values (because I'm OCD like that). I completely forgot the browser will not send inputs that are disabled.

Sorry for wasting your time everyone. Next time I'll post the code that unbinds the event, which would show you what I've just found.

Edit: What's worse is that I knew the form was submitting because I was being redirected to the action location. gg.

Jared
  • 2,978
  • 4
  • 26
  • 45