如何让js控制表单的提交和不提交?

<html> <script> function validate(){ var username = document.f1.username.value; if(username == ""){ alert("用户名不能为空"); return false; } } </script> <body> <form name="f1" method="post" onsubmit="return validate();"> <input type="text" name="username"> <input type="submit" type="提交"> </form> </body></html>
现在这里如果return false的话,form还是提交了一下,请问如何让它不提交??

<html> <script> function validate(){ var username = document.f1.username.value; if(username == ""){ alert("用户名不能为空"); return false; } } else{ document.forms["f1"].submit(); } </script> <body> <form name="f1" method="post"> <input type="text" name="username"> <input type="button" type="提交" onClick="validate()"> </form> </body></html>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-01
你这样写应该是正确的,只要返回是false就不提交的,我也是正常这样去表单验证,
<input type="submit" type="提交"> 是不是你这就话写错了?本回答被网友采纳
第2个回答  2013-06-01
日你用在submit上就不行了