How do you know when to return false from a jQuery function?

前端 未结 1 963
时光说笑
时光说笑 2021-01-06 21:59

\"return false\" seems to work in the submitHandler but I\'m not sure why.

function submitHandler() {
    $.post(         


        
相关标签:
1条回答
  • 2021-01-06 22:30

    You should return false when:

    • You want to prevent the default event action

      • The onsubmit event for example, the default action of this event is send the form data to the server, or an onclick event on an anchor element, the default event action is to follow the anchor's HREF.
    • You want to stop the event bubbling.

    Returning false from an event handler, is like executing both event.preventDefault(); and event.stopPropagation();.

    0 讨论(0)
提交回复
热议问题