2012-04-03 73 views
0

我需要一些帮助!Javascript/jQuery返回false不会停止执行

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

回答

3

让你的功能需要一个事件参数,然后调用event.preventDefault()

例如

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

这个方法的问题是,最终的检查('如果($('#supDoc ...')仍然会执行,可能导致多个警报。 – sje397 2012-04-03 13:14:18

+0

简单解决e,而不是警告错误,将警报消息放入变量中,检查变量是否为空,如果不是,则警告其内容。或者在foreach之前放置第一个提醒;) – 2012-04-03 13:17:47

1

的问题是,你的each循环里面,return false刚刚离开你传递给each功能。根据docs,这将退出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; 
    // ... 
}); 
+0

谢谢!帮助我解决同一类问题 – 2014-09-15 21:32:56

0

jquery each documentation

我们可以通过将回调函数返回false打破在一个特定的迭代。每个$()循环。返回非错误与for循环中的continue语句相同;它会立即跳到下一个迭代。

我认为你必须把你的回报虚假的每个循环,而不是查找()

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; 
}); 

希望它能帮助。

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; 
}); 
相关问题