最近搜索

js for 中断 终止 js中断函数的执行。

浏览:7
管理员 2025-08-06 21:38





function  save(){
    var index = layer.msg('提交中,请稍候', {
        icon : 16,
        time : false,
        shade : 0.8
    });

    var arr = [];
    var index = 1;
    try {
        $("#price_list tbody tr").each(function () {
            var taocanId = $(this).find('select').eq(0).val();
            var date = $(this).find('input').eq(0).val();
            var price = $(this).find('input').eq(1).val();
            date = date.trim();
            price = price.trim();
            var obj = {};
            obj.taocanId = taocanId;
            obj.date = date;
            obj.price = price;
            if(!isValidDate(date)){
                layer.closeAll();
                layer.msg('第'+index+'行日期格式不对', {icon:2});
                throw {stop: true};  // 抛出异常跳出循环
            }
            //判断价格是否正确
            if(price=='0'||isEmpty(price) || !isValidNonNegativeNumber(price)){
                layer.closeAll();
                layer.msg('第'+index+'行价不对', {icon:2});
                throw {stop: true};  // 抛出异常跳出循环  后面js不执行。
            }
            arr.push(obj);
            index++;
        });
    } catch(e) {
        if(e.stop) return;
    }

    console.log(arr);
    console.log(JSON.stringify(arr));

    $.post("/admin/goods/price/add", {goodsId: "[[${param.goodsId}]]",json:JSON.stringify(arr)}, function(result) {
        if (result.success) {
            window.parent.closeDlg(result.msg);
        } else {
            layer.msg(result.msg);
        }
    }, 'json');
}



   // 使用 some 检查是否有元素
    if (this.data.selectGoodsList.some(function (item) {
        //判断num是数字 不能是0 不能是负
        if (!isNum1(item.xiaoshou_num)) {
          wx.showToast({
            title: "第" + row_num + "行,请输入正确的数量",
            icon: 'none',
            duration: 2000
          })
          return true;
        }
        //判断 数字 或者浮点
        if (!isNum2(item.xiaoshou_price)) {
          wx.showToast({
            title: "第" + row_num + "行,请输入正确的单价",
            icon: 'none',
            duration: 2000
          })
          return true;
        }
        data.push({
          num: item.xiaoshou_num,
          goods_id: item.id,
          danjia: item.xiaoshou_price,
          zongjia: item.total,
        });
        row_num++;
      })) { // some 返回 true  相当于if判断是true  终止 执行return
      return; // 终止整个函数 fun 的执行
    }


联系站长

站长微信:xiaomao0055

站长QQ:14496453