jquery each 跳出及返回问题

无意中用到jquery 中的each,源程序是这样写的
function verify(){

    $("input").each(function (i,n){ 
        var search_eml = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
        var isMobile=/^(?:13\d|15\d)\d{5}(\d{3}|\*{3})$/;   
        var isPhone=/^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
        if($(this).val().trim()==''){
            alert($(this).attr("placeholder")+"不能为空");
            return false;
        }
        if($(this).attr("id")=='tel'){
            if($(this).val().trim().length!=11){       
                alert("请在'"+$(this).attr("placeholder")+"'输入正确格式 !");
                $(this).focus();
                return false;
            }
        }

        if($(this).attr("id")=='email'){
            if(!search_eml.test($(this).val())){       
                alert("请在'"+$(this).attr("placeholder")+"'输入正确格式 !");
                $(this).focus();
                return false;
            }
        }

        })
        return true;
    }

}

原本以为会在调用处返回true或false但是结果是在调用处没有返回任何东西,搜索关于each的资料时发现  
break; 等同于  return false;
continue; 等同于 return true;
也就是说 return ture 或 return false 表示的是当前循环是否跳出,是否继续,所以做出如下修改.
function verify(){

    var v = '1';
    $("input").each(function (i,n){ 

        var search_eml = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;

        var isMobile=/^(?:13\d|15\d)\d{5}(\d{3}|\*{3})$/;   

        var isPhone=/^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;

        if($(this).val().trim()==''){

            alert($(this).attr("placeholder")+"不能为空");

            v = '0';

            return false;

        }

        if($(this).attr("id")=='tel'){

            if($(this).val().trim().length!=11){       

                alert("请在'"+$(this).attr("placeholder")+"'输入正确格式 !");

                $(this).focus();

                v = '0';

                return false;

            }

        }

        if($(this).attr("id")=='email'){

            if(!search_eml.test($(this).val())){       

                alert("请在'"+$(this).attr("placeholder")+"'输入正确格式 !");

                $(this).focus();

                v = '0';

                return false;

            }

        }

        if(v=='0'){

            return false

        }

        })

    if(v=='0'){
    return false
    }else{
    return true;
    }

}

在jquery each 中想要跳出当前循环(break)需要使用 return false,想要执行下一个(continue)使用return true;所以在你在each的内置函数中写return false时是不会返回调用函数处的.
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值