Javascript/jQuery返回false不会停止执行

Javascript / jQuery return false does not stop execution

本文关键字:执行 false jQuery 返回 Javascript      更新时间:2023-09-26

我有一个代码块,它在允许表单提交之前检查某些元素,循环通过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;
});