Blogger Information
Blog 11
fans 0
comment 0
visits 6696
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
表单初体验
向日葵
Original
381 people have browsed it

用html实现一个简单的用户注册界面,并实现密码的查看与隐藏

我们要用到的html标签有:form,他的属性有两个比较重要——method(指定表单的提交方式,get或post),action(指定表单提交的后端处理文件,如:register.php);label+input,一对标签可以实现绑定,其中input有很多类型,如:text文本类型、password密码类型、email邮箱类型、checkbox多选框、radio单选框,还有一种特殊的表单:select下拉菜单;
那我们用上面的input类型,能形成一个什么样的用户注册页面呢,如下图所示:

那么实现界面上这些功能的代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. </head>
  9. <body>
  10. <!-- 用户名、密码、性别、爱好、邮箱、下拉列表 -->
  11. <form action="register.php" method="post" style="display: grid; gap: 10px;">
  12. <div>
  13. <label for="username">用户名:</label>
  14. <input
  15. type="text"
  16. name="username"
  17. id="username"
  18. required
  19. value="admin"
  20. placeholder="英文6-8位"
  21. />
  22. </div>
  23. <div>
  24. <label for="password">密码:</label>
  25. <input
  26. type="password"
  27. name="password"
  28. id="password"
  29. required
  30. placeholder="英文+数字6-12位"
  31. />
  32. <button onclick="showPassword(this,this.form.password)" type="button">
  33. 显示
  34. </button>
  35. </div>
  36. <div>
  37. <label for="email">邮箱:</label>
  38. <input type="email" name="email" id="email" placeholder="请输入邮箱" />
  39. </div>
  40. <div>
  41. <label for="secret">性别:</label>
  42. <input type="radio" name="gender" id="male" value="male" />
  43. <label for="male"></label>
  44. <input type="radio" name="gender" id="female" value="female" />
  45. <label for="female"></label>
  46. <input type="radio" name="gender" id="secret" value="secret" checked />
  47. <label for="secret">保密</label>
  48. </div>
  49. <div>
  50. <label>爱好:</label>
  51. <input type="checkbox" name="hobby[]" id="football" value="football" />
  52. <label for="football">足球</label>
  53. <input
  54. type="checkbox"
  55. name="hobby[]"
  56. id="basketball"
  57. value="basketball"
  58. />
  59. <label for="basketball">蓝球</label>
  60. <input
  61. type="checkbox"
  62. name="hobby[]"
  63. id="pingpangball"
  64. value="pingpangball"
  65. />
  66. <label for="pingpangball">乒乓球</label>
  67. <input
  68. type="checkbox"
  69. name="hobby[]"
  70. id="music"
  71. value="music"
  72. checked
  73. />
  74. <label for="music">音乐</label>
  75. </div>
  76. <label for="identity">用户身份:</label>
  77. <select name="level" id="identity">
  78. <option value="1" checked>普通会员</option>
  79. <option value="2">高级会员</option>
  80. <option value="3">永久会员</option>
  81. </select>
  82. <div>
  83. <button type="submit">提交</button>
  84. <button type="reset">重置</button>
  85. </div>
  86. </form>
  87. <script>
  88. function showPassword(btn, ele) {
  89. if (ele.type === 'password') {
  90. ele.type = 'text'
  91. btn.textContent = '隐藏'
  92. } else {
  93. ele.type = 'password'
  94. btn.textContent = '显示'
  95. }
  96. }
  97. </script>
  98. </body>
  99. </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