$('form').submit(function() {
alert($(this).serialize());
return false; // return true;
});
what's the difference for this form submission function between return false
and true
?
$('form').submit(function() {
alert($(this).serialize());
return false; // return true;
});
what's the difference for this form submission function between return false
and true
?
4 Answers
Reset to default 8If you return false
from the submit event, the normal page form POST will not occur.
return false
, don't do the form's default action. return true
, do the form's default action.
It's also better to do
$('form').submit(function(e) {
alert($(this).serialize());
e.preventDefault();
});
As already mentioned, returning false stops the event from "bubbling up". If you want the full details, take a look at the API documentation for bind()
: http://api.jquery.com/bind/.
"Returning false from a handler is equivalent to calling both .preventDefault() and .stopPropagation() on the event object."
return false; // cancel submit
return true; // continue submit
return true
is the default. Having noreturn
statement will returnundefined
. More correct would be that if the return value is any other thanfalse
, the submit event is processed normally. – Felix Kling Commented May 12, 2011 at 13:16