Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript/jQuery返回false不会停止执行_Javascript_Jquery - Fatal编程技术网

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

Javascript/jQuery返回false不会停止执行,javascript,jquery,Javascript,Jquery,我有一个代码块,在允许表单提交之前检查某些元素,循环循环通过OK 我预计,或者至少希望它第一次发现问题时,会显示警报,然后停止执行。实际发生的是,警报显示多次,一直显示到最后一次,只有最后一次实际返回false 我不明白为什么,我做错了什么?下面是JavaScript $('#deliverInForm').submit(function(){ $('.childRow').each(function(){ if($(this).find('.thisBinName').

我有一个代码块,在允许表单提交之前检查某些元素,循环循环通过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;
});
$('#deliverienform')。提交(函数(){
$('.childRow')。每个(函数(){
if($(this).find('.thisBinName').val()!=''this.find('.unitQty').val()!=''){
if($(this).find('.thisBinName').val()=''){
警报('您输入了数量但没有仓位,请更正并重试!');
返回false;
}else if($(this).find('.unitQty').val()=''){
警报('您输入了一个箱子,但没有数量,请更正并重试!');
返回false;
}else if($(this).find('.unitQty').val()==0){
警报('无法将零单位移动到间隔中,请更正并重试!');
返回false;

}否则,如果($(this).find('.unitQty').val()使函数接受事件参数,则调用

e、 g

$(“#deliverienform”).submit(函数(事件){
$('.childRow')。每个(函数(){
if($(this).find('.thisBinName').val()!=''this.find('.unitQty').val()!=''){
if($(this).find('.thisBinName').val()=''){
警报('您输入了数量但没有仓位,请更正并重试!');
event.preventDefault();
返回false;
}else if($(this).find('.unitQty').val()=''){
警报('您输入了一个箱子,但没有数量,请更正并重试!');
event.preventDefault();
返回false;
}else if($(this).find('.unitQty').val()==0){
警报('无法将零单位移动到间隔中,请更正并重试!');
event.preventDefault();
返回false;

}否则,如果($(this).find('.unitQty').val()使函数接受事件参数,则调用

e、 g

$(“#deliverienform”).submit(函数(事件){
$('.childRow')。每个(函数(){
if($(this).find('.thisBinName').val()!=''this.find('.unitQty').val()!=''){
if($(this).find('.thisBinName').val()=''){
警报('您输入了数量但没有仓位,请更正并重试!');
event.preventDefault();
返回false;
}else if($(this).find('.unitQty').val()=''){
警报('您输入了一个箱子,但没有数量,请更正并重试!');
event.preventDefault();
返回false;
}else if($(this).find('.unitQty').val()==0){
警报('无法将零单位移动到间隔中,请更正并重试!');
event.preventDefault();
返回false;

}否则,如果($(this).find('.unitQty').val()问题是在
每个
循环中,
返回false
只会退出传递给
每个
的函数。根据,这将退出
每个
循环,但不会退出提交处理程序

你可以做一些像这样丑陋的事情:

$('#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;
  // ...
});
$('#deliverienform')。提交(函数(){
var exit=false;
$('.childRow')。每个(函数(){
if($(this).find('.thisBinName').val()!=''this.find('.unitQty').val()!=''){
if($(this).find('.thisBinName').val()=''){
警报('您输入了数量但没有仓位,请更正并重试!');

exit=true;//问题是在
每个
循环中,
返回false
只会退出传递给
每个
的函数。根据,这将退出
每个
循环,但不会退出提交处理程序

你可以做一些像这样丑陋的事情:

$('#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;
  // ...
});
$('#deliverienform')。提交(函数(){
var exit=false;
$('.childRow')。每个(函数(){
if($(this).find('.thisBinName').val()!=''this.find('.unitQty').val()!=''){
if($(this).find('.thisBinName').val()=''){
警报('您输入了数量但没有仓位,请更正并重试!');
退出=真;//从

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

我认为您必须将return false移动到each循环中,而不是在find()中

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

我认为您必须将return false移动到each循环中,而不是在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;
});
$('#deliverienform')。提交(函数(){
var errLog='';
$('.childRow')。每个(函数(){
if($(this).find('.thisBinName').val()!=''this.find('.unitQty').val()!=''){
if($(this).find('.thisBinName').val()=''){
errLog+=“您输入了数量,但没有仓位,请更正后重试!\n”;
}
if($(this).find('.unitQty').val()=''){
errLog+='您输入了一个箱子,但没有数量,请更正后再试!\n';
}
if($(this).find('.unitQty').val()==0){
errLog+=“无法将零个单位移动到间隔中,请更正并重试!”;
}
如果($(this).find('.unitQty').val()请尝试以下操作:

$('#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;
});
$('#deliverienform')。提交(函数(){
var errLog='';
$('.childRow')。每个(函数(){
if($(this).find('.thisBinName').val()!=''this.find('
$(".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;
});
$(".link").submit(function(){
    throw new Error("foo");
    return false; // never happens form submits as if returning true;
});