简体   繁体   中英

break from .each jquery

how can i break from jquery each with out 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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