无意中用到jquery 中的each,源程序是这样写的
原本以为会在调用处返回true或false但是结果是在调用处没有返回任何东西,搜索关于each的资料时发现
break; 等同于 return false;
continue; 等同于 return true;
也就是说 return ture 或 return false 表示的是当前循环是否跳出,是否继续,所以做出如下修改.
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时是不会返回调用函数处的.