function a() {
$("input").each(function() {
if (this.value =="") {
alert('1');return false;
}
}
alert('2');
});
当input为""时,总是有alert('2')出来.
我觉得奇怪,不是已经return false;了么?原来,each里面是有function()的,这里这个return false;只是终止each里面的function()而不是each外面的function();
解决方法,就是设定一个标识,
if(!flag) return false;
让它执行.
var flag=true;
function a() {
$("input").each(function() {
if (this.value =="") {
alert('1');
flag=false;
return false;
}
});
if(!flag) return false;
alert('2');
}
在回调函数里return false即可,大多数jQuery都是如此的
===================================
返回 'false' 将停止循环 (就像在普通的循环中使用 'break')。
返回 'true' 跳至下一个循环(就像在普通的循环中使用'continue')。