繁体   English   中英

打破.each jquery

[英]break from .each jquery

我怎么能从jquery中断出来return FALSE

考虑以下

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

}

有什么工作吗?

在不改变功能的情况下,只要在循环中获得所需内容,就可以在循环中将标志设置为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

}

你可以继续继续。 所以,不要做任何事情:

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

不过,我不确定为什么你不会为了破坏而return false

暂无
暂无

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

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