第 10 章 表单元素[上] - 水之原
学习要点:
1.表单元素总汇
2.表单元素解析
主讲教师:李炎恢
本章主要探讨 HTML5 中表单元素,表单元素用于获取用户的输入数据。
一.表单元素总汇
在 HTML5 的表单中,提供了各种可供用户输入的表单控件。
元素名称 |
说明 |
form |
表示 HTML 表单 |
input |
表示用来收集用户输入数据的控件 |
textarea |
表示可以输入多行文本的控件 |
select |
表示用来提供一组固定的选项 |
option |
表示提供提供一个选项 |
optgroup |
表示一组相关的 option 元素 |
button |
表示可用来提交或重置的表单按钮(或一般按钮) |
datalist |
定义一组提供给用户的建议值 |
fieldset |
表示一组表单元素 |
legend |
表示 fieldset 元素的说明性标签 |
label |
表示表单元素的说明标签 |
output |
表示计算结果 |
二.表单元素解析
1.定义表单
<span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">method</span><span style="color: #0000ff;">="post"</span><span style="color: #ff0000;"> action</span><span style="color: #0000ff;">="http://www.haosou.com/"</span><span style="color: #0000ff;">></span> <br><span style="color: #0000ff;"> <span style="color: #800000;">button</span><span style="color: #0000ff;">></span>提交<span style="color: #0000ff;"></span><span style="color: #800000;">button</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span></span></span>
解释:
元素名称 |
说明 |
action |
表示表单提交的页面 |
method |
表示表单的请求方式:有 POST 和 GET 两种,默认 GET |
enctype |
表示浏览器对发送给服务器的数据所采用的编码格式。有三种:application/x-www-form-urlencoded(默认编码,不能将文件上传到服务器)、multipart/form-data(用于上传文件到服务器)、text/plain(未规范的编码,不建议使用,不同浏览器理解不同) |
name |
设置表单名称,以便程序调用 |
target |
设置提交时的目标位置:_blank、_parent、_self、_top |
autocomplete |
设置浏览器记住用户输入的数据,实现自动完成表单。默认为 on 自动完成,如果不想自动完成则设置 off。 |
novalidate |
设置是否执行客户端数据有效性检查,后面课程讲解。 |
//使用 get 提交数据
method="get"
//丧失自动提示功能
autocomplete="off"
//使用_blank 新建目标
target="_blank"
2.表示用户输入数据
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="user"</span><span style="color: #0000ff;">></span></span>
解释:元素默认情况会出现一个单行文本框,有五个属性。
属性名称 |
说明 |
autofocus |
让光标聚焦在某个 input 元素上,让用户直接输入 |
disabled |
禁用 input 元素 |
autocomplete |
单独设置 input 元素的自动完成功能 |
form |
让表单外的元素和指定的表单挂钩提交 |
type |
input 元素的类型,内容较多,将在下节课展开讲解 |
name |
定义 input 元素的名称,以便接收到相应的值 |
//聚焦光标
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="user"</span><span style="color: #ff0000;"> autofocus</span><span style="color: #0000ff;">></span></span>
//禁用 input
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="user"</span><span style="color: #ff0000;"> disabled</span><span style="color: #0000ff;">></span></span>
//禁止自动完成
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="user"</span><span style="color: #ff0000;"> autocomplete</span><span style="color: #0000ff;">="off"</span><span style="color: #0000ff;">></span></span>
//表单外的 input
<span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">method</span><span style="color: #0000ff;">="get"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="register"</span><span style="color: #0000ff;">></span><span style="color: #000000;"> ... </span><span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="email"</span><span style="color: #ff0000;"> form</span><span style="color: #0000ff;">="register"</span><span style="color: #0000ff;">></span></span></span>
3.添加说明标签
<span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="user"</span><span style="color: #0000ff;">></span>用户名:<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="user"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="user"</span><span style="color: #0000ff;">></span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span></span></span></span>
解释:
4.对表单进行编组
<span style="color: #0000ff;"><span style="color: #800000;">fieldset</span><span style="color: #0000ff;">></span>...<span style="color: #0000ff;"></span><span style="color: #800000;">fieldset</span><span style="color: #0000ff;">></span></span>
解释:
//普通按钮
<span style="color: #0000ff;"><span style="color: #800000;">button </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="button"</span><span style="color: #0000ff;">></span>按钮<span style="color: #0000ff;"></span><span style="color: #800000;">button</span><span style="color: #0000ff;">></span> </span>
对于 type 属性为 submit 时,按钮还会提供额外的属性。
属性名称 |
说明 |
form |
指定按钮关联的表单 |
formaction |
覆盖 form 元素的 action 属性 |
formenctype |
覆盖 form 元素的 enctype 属性 |
formmethod |
覆盖 form 元素的 method 属性 |
formtarget |
覆盖 form 元素的 target 属性 |
formnovalidate |
覆盖 form 元素的 novalidate 属性 |
//表单外关联提交
<span style="color: #0000ff;"><span style="color: #800000;">button </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> form</span><span style="color: #0000ff;">="register"</span><span style="color: #0000ff;">></span>提交<span style="color: #0000ff;"></span><span style="color: #800000;">button</span><span style="color: #0000ff;">></span></span>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex
