search
HomeWeb Front-endHTML Tutorial第 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>

解释:

元素可以将一些表单元素组织在一起,形成一个整体。

 

属性名称

说明

name

给分组定义一个名称

form

让表单外的分组与表单挂钩

disabled

禁用分组内的表单

 

5.添加分组说明标签

<span style="color: #0000ff;"><span style="color: #800000;">fieldset</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">legend</span><span style="color: #0000ff;">></span><span style="color: #000000;">
        注册表单
    </span><span style="color: #0000ff;"></span><span style="color: #800000;">legend</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">fieldset</span><span style="color: #0000ff;">></span></span></span>

解释:元素给分组加上一个标题。

 

6.添加按钮 

<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: #0000ff;">></span><span style="color: #800000;">button</span><span style="color: #0000ff;">></span></span>

解释:

值名称

说明

submit

表示按钮的作用是提交表单,默认

reset

表示按钮的作用是重置表单

button

表示按钮为一般性按钮,没有任何作用

 

//提交表单 

<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: #0000ff;">></span>提交<span style="color: #0000ff;"></span><span style="color: #800000;">button</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;">="reset"</span><span style="color: #0000ff;">></span>重置<span style="color: #0000ff;"></span><span style="color: #800000;">button</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>

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version