什么"返回false;"做? [英] What does "return false;" do?

查看:237
本文介绍了什么"返回false;"做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个网页,用户可以在其中输入存储在数据库中,然后检索和打印使用页面 AJAX 的日志条目。我还是很新的 AJAX ,想知道是否有人能请向我解释什么返回false; 做的我的code结束了吗?并且它甚至是必要的?

如果我把第二个AJAX code后的返回false 的code不起作用!能否请您解释一下为什么?

  //处理,而不需要刷新页面提交表单
$('#FormSubmit)。递交(函数(五){
    //存储的今天的数据输入
    。VAR log_entry = $(#LogEntry)VAL();
    // prevent无法正常提交表单
    即preventDefault();

    $阿贾克斯({
        键入:POST,
        网址:behind_curtains.php,
        数据: {
            logentry:log_entry
        },
        成功:函数(){
            警报(log_entry);
            //提交后清空文本框
            $('#LogEntry)VAL()。
            // presents successs文本,然后淡出出来
            $(#进入-登录成功)HTML(您的内容已经进入了。)。
            $(#进入-登录成功)显示()淡出(3000)。
        }
    });
    //在submittion打印页上新的日志条目
    $阿贾克斯({
        键入:POST,
        网址:/wp-content/themes/childOfFanwood/traininglog_behind_curtains.php,
        数据: {
            log_entries_loop:真
        },
        成功:功能(数据){
            警报(数据);
            $(#日志条目容器)HTML()。
            $(#日志条目容器)HTML(数据);
        }
    });
    返回false;
});

 

解决方案

什么我会写在这里是如此的 jQuery的事件
对于香草JavaScript事件读@ T.J。克劳德评论在回答底部


返回false 里面的回调prevents的默认行为。例如,在一个提交事件,它不提交表单。

返回false 也停止冒泡,所以元素的父母不会知道事件发生。

返回false 等同于事件。preventDefault() + event.stopPropagation()

当然,所有code中的返回XXX后存在行不会被执行。 (与所有的编程语言,我知道)

或许对您有所帮助:
停止事件冒泡 - 提高性能,


A 真正的的演示讲解的的区别返回false 事件。preventDefault()

标记:

 < D​​IV ID =theDiv>
    <形式ID =theForm>
        <输入类型=提交值=提交/>
    < /形式GT;
< / DIV>
 

JavaScript的:

  $('#theDiv)。递交(函数(){
    警报(DIV!');
});
$('#theForm)。递交(函数(五){
    警报(表格!');
    即preventDefault();
});
 

现在......,当用户提交表单,第一个处理程序的形式提交,其中 preventDefault() - >形式将不提交,但该事件泡到div,触发它的提交处理。

现场演示

现在,如果表单提交的处理程序将取消与冒泡返回false

  $('#theDiv)。递交(函数(){
    警报(DIV!');
});
$('#theForm)。递交(函数(事件){
    警报(表格!');
    返回false;
    // 要么:
    。事件preventDefault();
    event.stopPropagation();
});
 

DIV的甚至不知道有一个表单提交。

现场演示


这是什么返回false 做的香草JavaScript事件

  

从DOM2处理程序返回false(的addEventListener ),什么事情都不做(没有prevents默认又不停止冒泡;从微软DOM2上下的处理程序(的attachEvent ),这prevents默认但不起泡;从DOM0处理器(的onclick =回......),这prevents默认的(前提是你有回报的该属性),但不起泡;从jQuery的事件处理程序,它两者,因为这是一个jQuery的事情详情和现场试验在这里 - TJ克劳德

I wrote a webpage where a user can enter a log entry that is stored on a database and then retrieved and printed on the page using ajax. I am still quite new to ajax and was wondering if somebody could please explain to me what does return false; do at the end of my code? and is it even necessary?

If I put the second ajax code after the return false the code does not work! can you please explain to me why?

//handles submitting the form without reloading page 
$('#FormSubmit').submit(function(e) {
    //stores the input of today's data
    var log_entry = $("#LogEntry").val();
    // prevent the form from submitting normally
    e.preventDefault();

    $.ajax({
        type: 'POST',
        url: 'behind_curtains.php',
        data: {
            logentry: log_entry
        },
        success: function() {
            alert(log_entry);
            //clears textbox after submission
            $('#LogEntry').val("");
            //presents successs text and then fades it out
            $("#entered-log-success").html("Your Entry has been entered.");
            $("#entered-log-success").show().fadeOut(3000);
        }
    });
    //prints new log entries on page upon submittion
    $.ajax({
        type: 'POST',
        url: '/wp-content/themes/childOfFanwood/traininglog_behind_curtains.php',
        data: {
            log_entries_loop: "true"
        },
        success: function(data) {
            alert(data);
            $("#log-entry-container").html("");
            $("#log-entry-container").html(data);
        }
    });
    return false;
});
​

解决方案

What I'll write here is true for jQuery events,
For vanilla javascript events read @T.J. Crowder comment at the bottom of the answer


return false inside a callback prevents the default behaviour. For example, in a submit event, it doesn't submit the form.

return false also stops bubbling, so the parents of the element won't know the event occurred.

return false is equivalent to event.preventDefault() + event.stopPropagation()

And of course, all code that exists after the return xxx line won't be executed. (as with all programming languages I know)

Maybe you find this helpful:
Stop event bubbling - increases performance?


A "real" demo to explain the difference between return false and event.preventDefault():

Markup:

<div id="theDiv">
    <form id="theForm" >
        <input type="submit" value="submit"/> 
    </form>
</div>​

JavaScript:

$('#theDiv').submit(function() {
    alert('DIV!');
});
$('#theForm').submit(function(e) {
    alert('FORM!');
    e.preventDefault();
});​

Now... when the user submit the form, the first handler is the form submit, which preventDefault() -> the form won't be submitted, but the event bubbles to the div, triggering it's submit handler.

Live DEMO

Now, if the form submit's handler would cancel the bubbling with return false:

$('#theDiv').submit(function() {
    alert('DIV!');
});
$('#theForm').submit(function(event) {
    alert('FORM!');
    return false;   
    // Or:
    event.preventDefault(); 
    event.stopPropagation();
});​

The div wouldn't even know there was a form submission.

Live DEMO


What does return false do in vanilla javascript events

return false from a DOM2 handler (addEventListener) does nothing at all (neither prevents the default nor stops bubbling; from a Microsoft DOM2-ish handler (attachEvent), it prevents the default but not bubbling; from a DOM0 handler (onclick="return ..."), it prevents the default (provided you include the return in the attribute) but not bubbling; from a jQuery event handler, it does both, because that's a jQuery thing. Details and live tests here – T.J. Crowder

这篇关于什么&QUOT;返回false;&QUOT;做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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