LoginSignup
39
26

More than 1 year has passed since last update.

jQuery .each()のループはbreakじゃ抜けれない

Last updated at Posted at 2017-01-23

今更だけどjQueryの.each()は「break」じゃ抜けれないんですって

いつものノリで「break」って書いたらエラーが出た。
え?breakじゃないの???
スクリーンショット 2017-01-23 13.35.17.png

![alt](https://qiita-image-store.s3.amazonaws.com/0/147005/5e9e4976-f066-4ce4-181a-dbc146ed4640.png =*100)

jQueryの.each()は「return false」で抜けるらしい

これが正解らしい。

break.js
$(".test").each(function () {
    if($(this).val === 1){
        alert("1ですね");
    }else{
        return false;
    }
}); 

因みにcontinueしたい場合は「return true」

continue.js
$(".test").each(function () {
    if($(this).val === 1){
        alert("1ですね");
    }else{
        return true;
    }
}); 

今まで.each()をブレイクしたことなかったのかなー俺

39
26
7

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
39
26