可以使用form表单的onsubmit方法,在提交表单之前,对表单或者网页中的数据进行检验。
onsubmit指定的方法返回true,则提交数据;返回false不提交数据。
<HTML><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><BODY><form action="" onsubmit="return toVaild()"><input type="text" id="ff"><input type="submit" id="submit" value ="提交"/></form></BODY><script language="javascript">function toVaild(){var val = document.getElementById("ff").value;alert(val);if(val == "可以提交"){alert("校验成功,之后进行提交");return true;}else{alert("校验失败,不进行提交");return false;}}</script></HTML>
上面的网页中,只有在id="ff"的输入框中输入“可以提交”,才进行表单提交;否则不提交。