html form表單提交前驗證
阿新 • • 發佈:2018-12-07
可以使用form表單的onsubmit方法,在提交表單之前,對錶單或者網頁中的資料進行檢驗。
onsubmit指定的方法返回true,則提交資料;返回false不提交資料。
<HTML> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <BODY> <form action="http://www.baidu.com" 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"的輸入框中輸入“可以提交”,才進行表單提交;否則不提交。