Table of Contents
HTML forms contain form elements.
HTML5 input type
Input restrictions
 
参考:
单选按钮输入
实例
提交按钮
Action 属性
Method 属性
何时使用 GET?
何时使用 POST?
Name 属性
用 <fieldset> 组合表单数据
HTML Form 属性

HTML form

Sep 14, 2016 am 09:24 AM
html form

HTML forms contain form elements.

Element definition HTML form

Form elements refer to different types of input elements, check boxes, radio buttons, submit buttons, etc.

HTML forms are used to collect different types of user input.

<form><span style="color: #000000;">
 .
form elements
 .
</span></form>
Copy after login
The element is the most important form element.

elements have many forms, according to different type attributes.

This is the type used in this chapter: (there are also password, checkbox, button, etc.)

Type Description
text Define regular text input.
radio Define radio button inputs (select one of multiple choices)
submit Define submit button (submit form)

HTML5 input type

HTML5 adds several new input types:

  • color
  • date
  • datetime
  • datetime-local
  • email
  • month
  • number
  • range
  • search
  • tel
  • time
  • url
  • week

Note: Input types not supported by old web browsers will be treated as input type text.

Input restrictions

Here are some commonly used input restrictions (some of which are new in HTML5):

Property Description
disabled Specifies that input fields should be disabled.
max Specifies the maximum value of the input field.
maxlength Specifies the maximum number of characters for the input field.
min Specifies the minimum value of the input field.
pattern Specifies the regular expression by which the input value is checked.
readonly Specifies that input fields are read-only (cannot be modified).
required Specifies that the input field is required (required).
size Specifies the width of the input field in characters.
step Specifies legal number intervals for input fields.
value Specify the default value of the input field.

 

参考:

  • HTML 输入类型

单选按钮输入

 定义单选按钮。

单选按钮允许用户在有限数量的选项中选择其中之一:

实例

<form>
<input type=<span style="color: #800000;">"</span><span style="color: #800000;">radio</span><span style="color: #800000;">"</span> name=<span style="color: #800000;">"</span><span style="color: #800000;">sex</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">male</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">checked</span>><span style="color: #000000;">Male
</span><br>
<input type=<span style="color: #800000;">"</span><span style="color: #800000;">radio</span><span style="color: #800000;">"</span> name=<span style="color: #800000;">"</span><span style="color: #800000;">sex</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">female</span><span style="color: #800000;">"</span>><span style="color: #000000;">Female
</span></form> 
Copy after login

 

提交按钮

 定义用于向表单处理程序(form-handler)提交表单的按钮。

表单处理程序通常是包含用来处理输入数据的脚本的服务器页面。

表单处理程序在表单的 action 属性中指定:

实例

<form action=<span style="color: #800000;">"</span><span style="color: #800000;">action_page.php</span><span style="color: #800000;">"</span>><span style="color: #000000;">
First name:</span><br>
<input type=<span style="color: #800000;">"</span><span style="color: #800000;">text</span><span style="color: #800000;">"</span> name=<span style="color: #800000;">"</span><span style="color: #800000;">firstname</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">Mickey</span><span style="color: #800000;">"</span>>
<br><span style="color: #000000;">
Last name:</span><br>
<input type=<span style="color: #800000;">"</span><span style="color: #800000;">text</span><span style="color: #800000;">"</span> name=<span style="color: #800000;">"</span><span style="color: #800000;">lastname</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">Mouse</span><span style="color: #800000;">"</span>>
<br><br>
<input type=<span style="color: #800000;">"</span><span style="color: #800000;">submit</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">Submit</span><span style="color: #800000;">"</span>>
</form> 
Copy after login

 

Action 属性

action 属性定义在提交表单时执行的动作。

向服务器提交表单的通常做法是使用提交按钮。

通常,表单会被提交到 web 服务器上的网页。

在上面的例子中,指定了某个服务器脚本来处理被提交表单:

<form action=<span style="color: #800000;">"</span><span style="color: #800000;">action_page.php</span><span style="color: #800000;">"</span>>
Copy after login

如果省略 action 属性,则 action 会被设置为当前页面。

 

Method 属性

method 属性规定在提交表单时所用的 HTTP 方法(GET 或 POST)

<form action=<span style="color: #800000;">"</span><span style="color: #800000;">action_page.php</span><span style="color: #800000;">"</span> method=<span style="color: #800000;">"</span><span style="color: #800000;">GET</span><span style="color: #800000;">"</span>><span style="color: #000000;">
或:

</span><form action=<span style="color: #800000;">"</span><span style="color: #800000;">action_page.php</span><span style="color: #800000;">"</span> method=<span style="color: #800000;">"</span><span style="color: #800000;">POST</span><span style="color: #800000;">"</span>>
Copy after login

何时使用 GET?

您能够使用 GET(默认方法):

如果表单提交是被动的(比如搜索引擎查询),并且没有敏感信息。

当您使用 GET 时,表单数据在页面地址栏中是可见的:

action_page.php?firstname=Mickey&lastname=Mouse
Copy after login

注释:GET 最适合少量数据的提交。浏览器会设定容量限制。

何时使用 POST?

您应该使用 POST:

如果表单正在更新数据,或者包含敏感信息(例如密码)。

POST 的安全性更加,因为在页面地址栏中被提交的数据是不可见的。

 

Name 属性

如果要正确地被提交,每个输入字段必须设置一个 name 属性。

本例只会提交 "Last name" 输入字段,因为“First name“没有设置 name 属性:

实例

<form action=<span style="color: #800000;">"</span><span style="color: #800000;">action_page.php</span><span style="color: #800000;">"</span>>
First name:
"text" value="Mickey">
Last name:
"text" name="lastname" value="Mouse">

"submit" value="Submit">
Copy after login

 

组合表单数据

 元素组合表单中的相关数据

 元素为

元素定义标题。

实例

<form action=<span style="color: #800000;">"</span><span style="color: #800000;">action_page.php</span><span style="color: #800000;">"</span>>
Personal information: First name:
"text" name="firstname" value="Mickey">
Last name:
"text" name="lastname" value="Mouse">

"submit" value="Submit">
Copy after login

  

HTML Form 属性

HTML

元素,已设置所有可能的属性,是这样的:

实例

<form action=<span style="color: #800000;">"</span><span style="color: #800000;">action_page.php</span><span style="color: #800000;">"</span> method=<span style="color: #800000;">"</span><span style="color: #800000;">GET</span><span style="color: #800000;">"</span> target=<span style="color: #800000;">"</span><span style="color: #800000;">_blank</span><span style="color: #800000;">"</span> accept-charset=<span style="color: #800000;">"</span><span style="color: #800000;">UTF-8</span><span style="color: #800000;">"</span><span style="color: #000000;">
ectype</span>=<span style="color: #800000;">"</span><span style="color: #800000;">application/x-www-form-urlencoded</span><span style="color: #800000;">"</span> autocomplete=<span style="color: #800000;">"</span><span style="color: #800000;">off</span><span style="color: #800000;">"</span> novalidate><span style="color: #000000;">
.
form elements
 .
</span></form> 
Copy after login

 

属性 描述
accept-charset 规定在被提交表单中使用的字符集(默认:页面字符集)。
action 规定向何处提交表单的地址(URL)(提交页面)。
autocomplete 规定浏览器应该自动完成表单(默认:开启)。
enctype 规定被提交数据的编码(默认:url-encoded)。
method 规定在提交表单时所用的 HTTP 方法(默认:GET)。
name 规定识别表单的名称(对于 DOM 使用:document.forms.name)。
novalidate 规定浏览器不验证表单。
target 规定 action 属性中地址的目标(默认:_self)。
Statement of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

What is the difference between <strong>, <b> tags and <em>, <i> tags? What is the difference between <strong>, <b> tags and <em>, <i> tags? Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

See all articles