博客列表 >form表单控件的使用教程和调用案例

form表单控件的使用教程和调用案例

昊森
昊森原创
2023年01月17日 18:13:12804浏览

表单的练手及文档

表单介绍

  • 表单是HTML中最重要的一部分
  • 99%的网络攻击都是通过表单发起的
  • 表单需要做到熟练操作/使用
    MDN关于表单的介绍

    常用标签

    1 <form>: 表单控件的容器
    2 <fieldset>: 表单控件分组容器
    3 <label>: 控件标签名称
    4 <input>: 输入控件,类型由 type 属性决定
    5 <select>+<option>: 下载列表框
    6 <input>+<datalist>+<option>: 预定义列表框
    7 <textarea>: 文本域(多行文本框)
    8 <button>: 按钮,默认同步提交(type=”submit”)

    常用属性

    form.id: 表单引用
    form.action: 表单处理脚本
    form.method: 表单提交方式(GET/POST)
    form.enctype: 表单数据编码方式
    form.onsubmit: 表单提交事件
    input.type: 输入控件类型
    input.type="text": 单行文本框(默认)
    input.type="email": 邮箱控件
    input.type="password": 密码控件(密文)
    input.type="number": 数值控件
    input.type="date": 日期控件
    input.type="color": 拾色器
    input.type="url": URL 控件
    input.type="search": 搜索框控件
    input.type="hidden": 隐藏域控件
    input.type="file": 文本域控件
    input.type="radio": 单选按钮
    input.type="checkbox": 复选框
    select.name+option.value: 下拉列表框
    input.list+datalist.id: 预定义列表框
    textarea.cols/rows: 文本域(多行文本框)
    button.type: 按钮(默认提交:type=”submit”)

    代码案例

    1. <!-- 表单登录与注册示例-->
    2. <!-- 创建一个表单控件 action表示控件内的信息提交到那个文件进行判断
    3. method表示这个控件用什么方式进行提交get/post
    4. -->
    5. <form action="login.php" method="post">
    6. <fieldset>
    7. <!-- 一个标题 -->
    8. <legend>用户登录</legend>
    9. <!-- for=id的内容即可实现联动,点击账号可定位到input输入框内
    10. 方便用户直接进行输入。
    11. -->
    12. <div class="user">
    13. <label for="user">账号:</label>
    14. <input type="text" id="user" name="user" placeholder="请输入账号" />
    15. </div>
    16. <!-- name内的内容为表单提交的时候的名称-password=用户输入的密码 -->
    17. <div class="password">
    18. <label for="paw" name="password">密码:</label>
    19. <input
    20. type="password"
    21. id="paw"
    22. name="password"
    23. placeholder="请输入密码"
    24. />
    25. </div>
    26. <!-- 登录按钮 -->
    27. <button>登录</button>
    28. </fieldset>
    29. </form>
    30. <form action="login.php" method="post">
    31. <fieldset>
    32. <legend>用户注册</legend>
    33. <div class="my-user">
    34. <!-- 使用占位符达到美观性,其实没啥用&#12288; -->
    35. <label for="user">账&#12288;&#12288;号:</label>
    36. <input type="text" id="user" placeholder="请输入账号" />
    37. </div>
    38. <div class="my-email">
    39. <label for="email">邮&#12288;&#12288;箱:</label>
    40. <input type="email" id="email" placeholder="请输入邮箱" />
    41. </div>
    42. <div class="password">
    43. <label for="paw">密&#12288;&#12288;码:</label>
    44. <input type="password" id="paw" placeholder="请输入密码" />
    45. </div>
    46. <div class="password">
    47. <label for="pasw">确认密码:</label>
    48. <input type="password" id="pasw" placeholder="请确认密码" />
    49. </div>
    50. <div class="my-date">
    51. <label for="date">生&#12288;&#12288;日:</label>
    52. <input type="date" id="date" />
    53. </div>
    54. <div class="my-date">
    55. <label for="">爱&#12288;&#12288;好:</label>
    56. <input type="radio" id="zuqiu" />
    57. <label for="zuqiu">足球</label>
    58. <input type="radio" id="lanqiu" />
    59. <label for="lanqiu">篮球</label>
    60. <input type="radio" id="yumaoqiu" />
    61. <label for="yumaoqiu">羽毛球</label>
    62. </div>
    63. <div class="jieshao">
    64. <label for="jieshao">自我介绍</label>
    65. <textarea
    66. name=""
    67. id="jieshao"
    68. cols="30"
    69. rows="10"
    70. class="cols"
    71. placeholder="介绍下自己吧!!"
    72. ></textarea>
    73. </div>
    74. <button>注册</button>
    75. </fieldset>
    76. </form>

    代码运行示例

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议