在提交表单

时间:2016-08-06 08:16:50

标签: javascript html dom

我的表单提交代码如下:



function test_final(){
  //condition 1 & condition 2 are return true then only form submitted true otherwise return false
  test_one();
  test_two();
  }


function test_one{ //condition 1;
  var one = document.getElementById('name').value;
  if(one == "") {return false;}
  else{return true;}
  }
function test_two{//condition 2;
  var two = document.getElementById('email').value;
  if(two == "") {return false;}
  else{return true;}
  }

<form onsubmit="return test_final()" action="test.php">
  <input type="text" id="name">
  <input type="text" id="email">
  <button>submit</button>
&#13;
&#13;
&#13;

在上面的代码中检查两个条件都满足然后只提交表单否则返回false。但在这种情况下它不工作..请更正我的代码或指导我正确的方式。任何帮助是欣赏..!

1 个答案:

答案 0 :(得分:0)

试试这个:

function test_final(){
  //condition 1 & condition 2 are return true then only form submitted true otherwise return false
  return test_one() && test_two();
  
}


function test_one(){ 

  var one = document.getElementById('name').value;

  if(one == "") {
     return false;
  }
  else{
     return true;
  }
}

function test_two(){  //condition 2;
  
  var two = document.getElementById('email').value;

  if(two == "") {
     return false;
  }
  else{
    return true;
  }
}
<form onsubmit="return test_final()" action="test.php">
  <input type="text" id="name">
  <input type="text" id="email">
  <button>submit</button>
  </form>