提交表单即使我有"返回false"

时间:2016-10-31 12:39:31

标签: return-value onsubmit

我有一个按钮,点击后调用 function1 调用另一个 function2 。如果function2满足条件,它应该返回false并在那里死亡但是它继续,返回到function1,执行其余的function1并提交表单。我是javascript的新手。

 <input type="button" value="Submit" onclick="function1()" />

 function function1() {
  function2();
  //execute rest of function
 }

 function2() {
 var this;
 var that;
 if(!this || !that) {
 alert("You did not fill out fields on the form under Tab1");
 return false;  //I thought it was supposed to stop here
 }
 } 

1 个答案:

答案 0 :(得分:1)

在function1中调用function2时,如果你想让function1在函数2返回false时停止,那么你必须将它停在INSIDE函数1中,如下所示:

function function1() {
  // If function2 returns false, then stop function1
  if (!function2()) return;
  //execute rest of function
 }

 function2() {
  var this;
  var that;
  if(!this || !that) {
   alert("You did not fill out fields on the form under Tab1");
   return false;  //I thought it was supposed to stop here
  }
 }