Leem
Leem

Reputation: 18318

what's the difference of return true or false here?

$('form').submit(function() {
  alert($(this).serialize());
  return false;  // return true;
});

what's the difference for this form submission function between return false and true?

Upvotes: 8

Views: 12946

Answers (4)

bmaland
bmaland

Reputation: 71

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."

Upvotes: 2

manji
manji

Reputation: 47978

return false;  // cancel submit
return true;   // continue submit

Upvotes: 1

stealthyninja
stealthyninja

Reputation: 10371

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();
});

Upvotes: 3

Matthew Abbott
Matthew Abbott

Reputation: 61599

If you return false from the submit event, the normal page form POST will not occur.

Upvotes: 8

Related Questions