2012-12-04 56 views
0

jQuery中是否存在类似于retun false;的函数中阻止父函数运行的方法?jQuery取消父功能

我有一个系统概述如下,当值不正确时需要停止。随着它,父功能需要停止。

parent.each(function(){ 

    child.each(function(){ 

     if (true) { 

      //stuff 
     } else { 

      return false and cancel parent.each(); 
     } 

    }); 

    otherChild.each(function(){ 

     if (true) { 

      //stuff 
     } else { 

      return false and cancel parent.each(); 
     } 
    }); 
}); 

回答

2
parent.each(function(){ 
    var canceled=false; 

    child.each(function(){ 

     if (true) { 

      //stuff 
     } else { 
      canceled=true; 

      return false; 
     } 

    }); 

    if(!canceled) otherChild.each(function(){ 

     if (true) { 

      //stuff 
     } else { 
      canceled=true; 
      return false; 
     } 
    }); 
    return !canceled; 
}); 
+1

我upvoted,因为这是取消父'。每个()'清洁方式,但OP应该注意,如果是使用'for'循环,而不是'。每() ',你可以在完成后回来,不必跟踪这个标志并在多个地方进行测试。这些类型的流程控制问题有时在'for'循环中简单处理,而不是'.each()'回调函数。 – jfriend00

+0

只是为了我的理解 - 最终回报有什么目的!取消服务器? – technopeasant

+0

@technopeasant将你从parent.each()循环中分离出来。 –