我给你一个简单的例子,你再适当修改一下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Onbeforeunload Test</title>
</head><body onbeforeunload="if (document.all.txthidden.value=='a') {alert('非法退出!');return false;}">
<form method="POST" action="http://www.microsoft.com">
  <p><input type="text" name="T1" size="20">
  <input type="submit" value="提交" name="btntest" onclick="document.all.txthidden.value='submit';return true;">
  <input type="reset" value="全部重写" name="B2">
  <input type="hidden" name="txthidden" size="10" value="a"></p>
</form>
</body>
</html>