jQuery - prevent default, then continue default
Solution 1:
Use jQuery.one()
Attach a handler to an event for the elements. The handler is executed at most once per element per event type
$('form').one('submit', function(e) {
e.preventDefault();
// do your things ...
// and when you done:
$(this).submit();
});
The use of one
prevent also infinite loop because this custom submit
event is detatched after the first submit.
Solution 2:
When you bind the .submit()
event to the form, and you do the things you want to do before returning (true), these things happen prior to the actual submission.
For example:
$('form').submit(function(){
alert('I do something before the actual submission');
return true;
});
Simple example
Another example on jquery.com: http://api.jquery.com/submit/#entry-examples
Solution 3:
I would just do:
$('#submiteButtonID').click(function(e){
e.preventDefault();
//do your stuff.
$('#formId').submit();
});
Call preventDefault
at first and use submit()
function later, if you just need to submit the form
Solution 4:
Using this way You will do a endless Loop on Your JS. to do a better way you can use the following
var on_submit_function = function(evt){
evt.preventDefault(); //The form wouln't be submitted Yet.
(...yourcode...)
$(this).off('submit', on_submit_function); //It will remove this handle and will submit the form again if it's all ok.
$(this).submit();
}
$('form').on('submit', on_submit_function); //Registering on submit.
I hope it helps! Thanks!
Solution 5:
$('#myform').on('submit',function(event){
// block form submit event
event.preventDefault();
// Do some stuff here
...
// Continue the form submit
event.currentTarget.submit();
});