您如何知道何时从jQuery函数返回false? [英] How do you know when to return false from a jQuery function?

查看:78
本文介绍了您如何知道何时从jQuery函数返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

" return false "似乎可以在submitHandler中使用,但是我不确定为什么.

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

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

});

我也不确定是否需要在#tip_click函数中添加" return false ",使其看起来像这样:

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

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

在什么时候"return false"要遵循什么规则?

What's the rule to follow here regarding when to "return false"?

推荐答案

在以下情况下应return false:

  • 您要阻止默认事件操作

  • You want to prevent the default event action

  • 例如,onsubmit事件,此事件的默认操作是将表单数据发送到服务器,或锚元素上的onclick事件,默认事件操作是跟随锚的HREF.
  • li>
  • 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.

您要停止事件冒泡.

从事件处理程序返回false,就像执行两个 event.preventDefault(); event.stopPropagation(); .

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

这篇关于您如何知道何时从jQuery函数返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆