I have a form which calls an ajax post, using the submit()
jquery method:
<script>
$(document).ready(function(){
$('#myform').submit(function(){
submitFunction(); /* does and Ajax post */
return false;
});
});
</script>
<form id="myform" action="thisaction" method="post">
<!-- ... -->
</form>
Now, the thing is, when that Ajax post succeeds, I want to cancel the jquery override
of my form submission, in order to let it do its normal action (thisaction
). I already tried this :
/* Inside submitFunction() */
$.ajax({
/* random data... */
success: function(jsonData)
{
$('#mylogin').submit(function(){
;
});
}
});
But when I debug it, the submitFunction()
is still called after the second submission... How do I disable this and let a normal
HTTP post to thisaction
occur?
You have to unbind the event.
$('#mylogin').submit(function(){
$(this).unbind("submit");
});
Here the documentation http://api.jquery.com/unbind/
In the submit handler, before the submitFunction()
:
$(this).off('submit')
And in the ajax success callback:
$('#myform').submit();
This will unbind the submit handler that returns false and re-submit the form when the ajax callback is called.
You can also try .one()
to bind a handler that will only be executed once:
$('#myform').one('submit', function() {...