javascript 这里 return true 或 false 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5978274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what's the difference of return true or false here?
提问by Leem
$('form').submit(function() {
alert($(this).serialize());
return false; // return true;
});
what's the difference for this form submission function between return false
and true
?
这个表单提交功能在 returnfalse
和之间有什么区别true
?
采纳答案by Matthew Abbott
If you return false
from the submit event, the normal page form POST will not occur.
如果false
从 submit 事件返回,则不会发生正常的页面表单 POST。
回答by stealthyninja
return false
, don't do the form's default action. return true
, do the form's default action.
return false
, 不要执行表单的默认操作。return true
, 执行表单的默认操作。
It's also better to do
这样做也更好
$('form').submit(function(e) {
alert($(this).serialize());
e.preventDefault();
});
回答by bmaland
As already mentioned, returning false stops the event from "bubbling up". If you want the full details, take a look at the API documentation for bind()
: http://api.jquery.com/bind/.
如前所述,返回 false 会阻止事件“冒泡”。如果您需要完整的详细信息,请查看以下 API 文档bind()
:http: //api.jquery.com/bind/。
"Returning false from a handler is equivalent to calling both .preventDefault() and .stopPropagation() on the event object."
“从处理程序返回 false 相当于在事件对象上调用 .preventDefault() 和 .stopPropagation()。”
回答by manji
return false; // cancel submit
return true; // continue submit