On my page, there is a form with id=save-questions-form
. I'm trying intercept form submit by the following way:
$('#save-questions-form').on('submit', handler);
There is a content of the handler function below (inside ajax, a post-request is sent to the form action):
function handler() {
$.ajax({...});
return false;
}
In this case, the form is sent twice. This option below doesn't help either.
function handler(event) {
event.preventDefault();
$.ajax({...});
return false;
}
But if I'm trying to do the same things without ajax
, for example like this:
function handler(event) {
return false;
}
then it works fine, and form isn't sent.
I solved this problem in the following way:
$('document').on('submit', '#save-questions-form', saveQuestionsSubmit);
In this case, everything works fine and form doesn't send twice.
But I don't understand how it works? Any ideas?