js the difference between 'return false' and 'return true'

js回调函数return false 阻止了默认行为;return true 继续执行默认行为;不返回值,是undefined。

除了return false,默认行为会正常执行。(下面的默认行为就是post表单)

 

栗子:

1.

$('form').submit(function(){
  alert($(this).serialize());
return false;// return true;
});

解释:
If you return false from the submit event, the normal page form POST will not occur.

也可以这样写,阻止默认行为的效果更好
$('form').submit(function(e){
  alert($(this).serialize());
  e.preventDefault();
});


http://stackoverflow.com/questions/5978274/whats-the-difference-of-return-true-or-false-here


2
. break/exit from a each() function
栗子:
$(xml).find("strengths").each(function(){
//Code
//How can i escape from this block based on a condition.
});

解决:
$(xml).find("strengths").each(function(){
if(iWantToBreak)
return false;
});

http://stackoverflow.com/questions/1799284/how-to-break-exit-from-a-each-function-in-jquery

 
 
 

posted on 2014-04-25 09:52  Sure_Yi  阅读(238)  评论(0编辑  收藏  举报

导航