2

I have a little php experience and would like to learn ajax jQuery. I've been watching several tutorials on submitting a form without refreshing the page. I notice people use different ways to prevent a form from submitting the normal way (with a page refresh):

  • with the event.preventDefault() function
  • using return false
  • In a script I'm trying to understand the JS script starts with:

    $("#submitBrandForm").unbind('submit').bind('submit', function() {

Does this also have the same functionality? This same script ends with 'return false', which confuses me (and makes me think the unbind - bind code has another purpose).

Thanks

1 Answers1

0

No, the third one doesn't do the same thing. unbind only removes any previously attached event handlers (via bind), it does not prevent the form from being submitted.

In your example unbind and bind are just used to replace the existing event handler(s). If you don't unbind first, bind will add a handler to the ones that are already attached. You'll also often see unbind('submit', ...) inside the handler for a submit event that programmatically submits the form, to prevent the function from calling itself endlessly, like in this answer.

By the way, bind and unbind have been deprecated, you should be using on and off.

Community
  • 1
  • 1
GertG
  • 959
  • 1
  • 8
  • 21