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

" " seems to work in , but I'm not sure why. return falsesubmitHandler

function submitHandler() {
    $.post($(this).attr('action'), $(this).serialize(), null, "script");
    return false;
}

$(document).ready(function () {

    $('#top_cat').submit(submitHandler);

    $("#tip_click").click(function() {
        $("#high_hat").submit(submitHandler);
    });

});

I'm also not sure that I need to add " " to the function so that it reads like this: return false#tip_click

    $("#tip_click").click(function() {
        $("#high_hat").submit(submitHandler);
        return false;
    });

Which rule should be followed here when " return false"?

+3
source share
1 answer

You should return falsewhen:

  • Do you want to prevent the default event action

    • An event onsubmit, for example, the default action of this event sends form data to the server or an event onclickin the binding element, the default action should follow the reference HREF.
  • .

false , event.preventDefault(); event.stopPropagation();.

+5

Source: https://habr.com/ru/post/1721263/


All Articles