Blogger Information
Blog 28
fans 0
comment 0
visits 12969
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
禁用表单默认提交的三种方式以及this.target,currentTarget的区别与联系演示
手机用户1594223549
Original
369 people have browsed it

1.输出结果

2.代码部分

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>表单事件</title>
  8. <link rel="stylesheet" href="form.css">
  9. </head>
  10. <body>
  11. <!-- 通过onsubmit="return false"禁用提交 -->
  12. <!-- <form action="login.php" method="post" id="login" onsubmit="return false"> -->
  13. <form action="login.php" method="post" id="login">
  14. <label class="title">用户登录</label>
  15. <label for="email">账号:</label>
  16. <input type="email" id="email" name="email" value="" autofocus />
  17. <label for="password">密码:</label>
  18. <input type="password" id="password" name="password" />
  19. <!-- form中的button,默认type="submit",改成type="button"就可以禁用 -->
  20. <!-- <button name="submit" type="button">登录</button> -->
  21. <!-- 禁用表单默认提交行为的3种方法
  22. 1. form.onsubmit = 'return false'
  23. 2. form.button.type = 'button'
  24. 3. event.preventDefault() -->
  25. <button name="submit">登录</button>
  26. </form>
  27. <script>
  28. // 事件对象来控制
  29. document.forms.login.submit.onclick = function (ev) {
  30. // 禁止默认提交行为
  31. ev.preventDefault();
  32. // 区分绑定和触发,事件主体二合一
  33. console.log(ev.target === ev.currenTarget);
  34. console.log(this === ev.target);
  35. // 当前事件主体的三种形式
  36. // 1. ev.target
  37. // 2. ev.currentTarget
  38. // 3. this
  39. console.log(this.form);
  40. // 非空验证
  41. if (email.value.trim().length === 0) {
  42. // trim过滤空的字符串
  43. alert('账号不能为空');
  44. email.focus();
  45. return false;
  46. } else if (password.value.trim().length === 0) {
  47. alert('邮箱不能为空');
  48. password.focus();
  49. return false;
  50. }
  51. };
  52. </script>
  53. </body>
  54. </html>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:整体效果不错, 非空验证时应是密码不能为空
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post