zoukankan      html  css  js  c++  java
  • 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;
                }

    本文仅供参考

  • 相关阅读:
    uwsgi配置
    sed_shell三剑客
    grep_shell三剑客
    awk_shell三剑客
    spring(二)
    spring(一)
    5G的科普
    应用层协议基础
    IP地址相关运算(如VLSM,超网汇总)
    ARP协议基础
  • 原文地址:https://www.cnblogs.com/wwdxf/p/6394802.html
Copyright © 2011-2022 走看看