jquery中each函数的return

$("#dishTypeTable tbody").find("tr").eq(0).siblings().each(function(index){
                var chooseNum = $(this).find("td").eq(1).find("input:checked").length;
                console.log(chooseNum);
                if (chooseNum == 0) {
                    layer.msg("每个门店至少选择一个参与活动的菜品种类");
                    return;
                }
            });

运行之后发现并不能起到终止方法的作用,debug发现在each函数中每次return,只是起到了跳出当次循环的作用.

百度之后发现,return true(return) 相当于continue,跳出当次循环

      return false 相当于 break,跳出当前循环

如果想要终止function,需要抛出异常进行捕捉,在catch块中进行return

try {
                $("#dishTypeTable tbody").find("tr").eq(0).siblings().each(function(index){
                    var chooseNum = $(this).find("td").eq(1).find("input:checked").length;
                    console.log(chooseNum);
                    if (chooseNum == 0) {
                        throw "每个门店至少选择一个参与活动的菜品种类";
                        return;
                    }
                });
            } catch (e) {
                layer.msg(e);
                return;
            }

本文仅供参考

posted on 2017-02-13 17:44  方季惟心  阅读(390)  评论(0编辑  收藏  举报

导航