Javascript/jQuery返回false不会停止执行
Javascript / jQuery return false does not stop execution
我有一个代码块,它在允许表单提交之前检查某些元素,循环通过OK。
我预计,或者至少希望它第一次发现问题时,会显示警报,然后停止执行。实际情况是,警报显示多次,一直到最后一次,只有最后一次真正返回false!
我不明白为什么,我做错了什么?JavaScript如下。
$('#deliverInForm').submit(function(){
$('.childRow').each(function(){
if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){
if($(this).find('.thisBinName').val()==''){
alert('You have entered a quantity but no Bin, please correct and try again!');
return false;
}else if($(this).find('.unitQty').val()==''){
alert('You have entered a Bin but no quantity, please correct and try again!');
return false;
}else if($(this).find('.unitQty').val()==0){
alert('Can''t move zero units into a bay, please correct and try again!');
return false;
}else if($(this).find('.unitQty').val()<0){
alert('Can''t deliver in minus units into a bay, please correct and try again!');
return false;
}
}
});
if($('#supDocNo').val()==''){
alert('Can''t leave Supplier Reference blank, please correct and try again!');
return false;
}
return true;
});
让函数接受一个事件参数,然后调用event.prventDefault()
例如
$('#deliverInForm').submit(function(event){
$('.childRow').each(function(){
if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){
if($(this).find('.thisBinName').val()==''){
alert('You have entered a quantity but no Bin, please correct and try again!');
event.preventDefault();
return false;
}else if($(this).find('.unitQty').val()==''){
alert('You have entered a Bin but no quantity, please correct and try again!');
event.preventDefault();
return false;
}else if($(this).find('.unitQty').val()==0){
alert('Can''t move zero units into a bay, please correct and try again!');
event.preventDefault();
return false;
}else if($(this).find('.unitQty').val()<0){
alert('Can''t deliver in minus units into a bay, please correct and try again!');
event.preventDefault();
return false;
}
}
});
if($('#supDocNo').val()==''){
alert('Can''t leave Supplier Reference blank, please correct and try again!');
event.preventDefault();
return false;
}
return true;
});
问题是在each
循环中,return false
刚刚退出您传递给each
的函数。根据文档,这将退出each
循环,但不会退出提交处理程序。
你可以做这样丑陋的事情:
$('#deliverInForm').submit(function(){
var exit = false;
$('.childRow').each(function(){
if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){
if($(this).find('.thisBinName').val()==''){
alert('You have entered a quantity but no Bin, please correct and try again!');
exit = true; // <----------------set flag
return false;
//...
});
if(exit) return false;
// ...
});
来自jquery的每个文档
我们可以在特定的迭代中通过使回调函数返回false来中断$.each()循环。返回非false与for循环中的continue语句相同;它将立即跳到下一次迭代。
我认为您必须将return false移到每个循环中,而不是在find()
试试这个:
$('#deliverInForm').submit(function(){
var errLog = '';
$('.childRow').each(function(){
if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){
if($(this).find('.thisBinName').val()==''){
errLog += 'You have entered a quantity but no Bin, please correct and try again!'n';
}
if($(this).find('.unitQty').val()==''){
errLog += 'You have entered a Bin but no quantity, please correct and try again!'n';
}
if($(this).find('.unitQty').val()==0){
errLog += 'Can''t move zero units into a bay, please correct and try again!';
}
if($(this).find('.unitQty').val()<0){
errLog += 'Can''t deliver in minus units into a bay, please correct and try again!';
}
}
});
if($('#supDocNo').val()==''){
errLog += 'Can''t leave Supplier Reference blank, please correct and try again!'n';
}
if( errLog != '' ) {
alert( errLog );
errLog = '';
return false;
}
return true;
});
希望能有所帮助。
事件处理程序函数需要返回值。
$(".link").submit(function(){
return false;
});
要显示第一个错误对话框并返回false,您必须执行以下操作。。。
$(".link").submit(function(){
var submit=true;
$("div").each(function(){
if(submit) // comment this out to show all dialogs
if(!condition){
submit = false;
alert("problem");
return ""; // return here is irrelevant
}
})
return submit;
});
还有一件事,如果你的函数抛出一个错误,它不会返回false,并且提交无论如何都会发生。。。
$(".link").submit(function(){
throw new Error("foo");
return false; // never happens form submits as if returning true;
});
相关文章:
- onsubmit返回false,但submit仍然执行
- Ajax-执行成功前返回false
- if(function()===false){执行此操作}
- 如果执行,尽管条件为 false
- 如果初始事件返回 false,则停止执行事件侦听器
- jQuery -- 为什么只执行第一个条件,或者如果第一个条件为 false,则根本不执行
- 如何在 js 中获取 false 作为返回后停止 php 执行
- Javascript/jQuery返回false不会停止执行
- 即使在函数中将returnValue设置为False之后,控制器动作仍在执行
- Javascript IF循环采用false,并使用jquery执行true循环
- 监视一个方法与西农.方法绑定到事件侦听器.方法已执行,但.calledOnce为false
- 从JavaScript返回false不会阻止HTML页面的执行
- JavaScript return false 不会阻止其他函数的执行
- 如何在javascript中停止执行返回false
- 条件为false,但if语句中的代码在JavaScript中执行
- 执行3个返回true/false的函数,如果所有函数都返回true,则执行其他操作,没有短路
- 如何使'返回false'防止在内联onclick事件处理程序中连续执行函数
- 如何在成功为false时停止jQuery .ajax()的执行
- 如果验证返回false,则执行javascript以阻止数据进入数据库
- Javascript 日期时间比较返回 false,但分支 stmt 仍然执行