JS示例33-表单事件(提交和重置事件)

  • 2019 年 11 月 7 日
  • 筆記

一、知识要点

onsubmit onreset

二、源码参考

<!DOCTYPE HTML>  <html>    <head>      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">      <title>无标题文档</title>      <script>          window.onload = function () {                var oForm = document.getElementById('form1');                // onsubmit : 当表单被提交的时候触发              oForm.onsubmit = function () {                  if (this.username.value == '') {                      alert('请填写用户名');                      return false;                  }              }                // onreset : 当表单要重置的时候触发              oForm.onreset = function () {                  return confirm('你确定要重置?');              }                confirm('你确定要重置?');              document.title = 1;            }      </script>  </head>    <body>      <form id="form1" action="http://www.baidu.com">          <input type="text" name="username" />          <input type="password" name="password" />          <!--<input type="button" value="提交" />-->          <input type="submit" value="提交" />          <input type="reset" value="重置" />      </form>  </body>    </html>

image.png