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

时间:2012-04-03 13:02:57

标签: javascript jquery

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

5 个答案:

答案 0 :(得分:3)

让你的函数接受一个事件参数,然后调用event.preventDefault()

e.g。

$('#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;
});

答案 1 :(得分:1)

问题是,在each循环中,return false只会退出您传递给each的函数。根据{{​​3}},这将退出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;
  // ...
});

答案 2 :(得分:0)

来自jquery each documentation

我们可以通过使回调函数返回false来在特定迭代中中断$ .each()循环。返回非false与for循环中的continue语句相同;它将立即跳到下一次迭代。

我认为你必须将你的返回false移动到每个循环而不是find()

答案 3 :(得分:0)

试试这个:

$('#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;
});

希望它有所帮助。

答案 4 :(得分:0)

事件处理函数需要返回值。

$(".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;
});