可以使用form表單的onsubmit方法,在提交表單之前紧显,對(duì)表單或者網(wǎng)頁(yè)中的數(shù)據(jù)進(jìn)行檢驗(yàn)讲衫。
onsubmit指定的方法返回true,則提交數(shù)據(jù)孵班;返回false不提交數(shù)據(jù)涉兽。
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<BODY>
<form action="" onsubmit="return check()">
<input type="text" id="txt">
<input type="submit" id="submit" value ="提交"/>
</form>
</BODY>
<script language="javascript">
function check(){
var val = document.getElementById("txt").value;
if(val == ""){
alert("值不能為空");
return false;
}
return true;
}
</script>
</HTML>