I am handling a form submit like this:
$('#some-form').submit(function(ev) {
var $form = $(this);
$.getJSON('/some/endpoint', function(data) {
if (data.somecondition) {
$form.submit(); // <- not doing what I want
}
});
return false;
});
So, I’m beginning an asynchronous getJSON call and then returning false to stop the form submission. Within the getJSON callback, under some condition, I want to actually submit the form. But triggering submit() just calls the handler again and repeats the process.
I know I can unbind the submit handler and then submit, but there’s got to be a better way, right? If there isn’t a better way, what’s the best way to structure this code to unbind the submit handler?
Turns out it’s actually very simple, just call
submit()on the non-extended version of the element:Example here: http://jsbin.com/eyuteh/7/edit#html
Caveat: See @Avi Pinto’s comment.