简体   繁体   English

打破.each jquery

[英]break from .each jquery

how can i break from jquery each with out return FALSE . 我怎么能从jquery中断出来return FALSE

consider the following 考虑以下

function check_quantity(){

  var _q = $(".quantity");

       _q.each( function(){

           if( some_condition ){

             break; // I WANT TO BREAK HERE AND NEED TO RETURN A TRUE INSTEAD OF FALSE
                    // for some reasons there is no way to continue 
           }

        } );

   return FALSE; // if condition failed

}

is there any work around ? 有什么工作吗?

Without changing your function, just set a flag to true in your loop if you get what you want in it. 在不改变功能的情况下,只要在循环中获得所需内容,就可以在循环中将标志设置为true。

function check_quantity(){

  var ret = false;
  var _q = $(".quantity");

       _q.each( function(){

           if( some_condition ){

             ret = true; // I WANT TO BREAK HERE AND NEED TO RETURN A TRUE INSTEAD OF FALSE
                    // for some reasons there is no way to continue 
           }

        } );

   return ret; // if condition failed

}

You could just keep continue'ing. 你可以继续继续。 So, just don't do anything: 所以,不要做任何事情:

q.each(function () {
    if(condition) {
      //don't do anything
    }
    else {
      //do stuff
    }
}

I'm not sure why you wouldn't just return false in order to break, though. 不过,我不确定为什么你不会为了破坏而return false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM