How to break out of jQuery each Loop

后端 未结 7 2247
遇见更好的自我
遇见更好的自我 2020-11-22 10:04

How do I break out of a jQuery each loop?

I have tried:

 return false;

In the loop but this did not work. Any ideas?

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 11:00

    I came across the situation where I met a condition that broke the loop, however the code after the .each() function still executed. I then set a flag to "true" with an immediate check for the flag after the .each() function to ensure the code that followed was not executed.

    $('.groupName').each(function() {
        if($(this).text() == groupname){
            alert('This group already exists');
            breakOut = true;
            return false;
        }
    });
    if(breakOut) {
        breakOut = false;
        return false;
    } 
    

提交回复
热议问题