Thẻ <form> HTML


Thí dụ

Một biểu mẫu HTML với hai trường đầu vào và một nút gửi:

<form action="/action_page.php" method="get">
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <label for="lname">Last name:</label>
  <input type="text" id="lname" name="lname"><br><br>
  <input type="submit" value="Submit">
</form>

Thêm các ví dụ "Hãy tự mình thử" bên dưới.


Định nghĩa và Cách sử dụng

Thẻ <form>được sử dụng để tạo một biểu mẫu HTML để người dùng nhập vào.

Phần <form>tử có thể chứa một hoặc nhiều phần tử biểu mẫu sau:


Hỗ trợ trình duyệt

Element
<form> Yes Yes Yes Yes Yes

Thuộc tính

Attribute Value Description
accept-charset character_set Specifies the character encodings that are to be used for the form submission
action URL Specifies where to send the form-data when a form is submitted
autocomplete on
off
Specifies whether a form should have autocomplete on or off
enctype application/x-www-form-urlencoded
multipart/form-data
text/plain
Specifies how the form-data should be encoded when submitting it to the server (only for method="post")
method get
post
Specifies the HTTP method to use when sending form-data
name text Specifies the name of a form
novalidate novalidate Specifies that the form should not be validated when submitted
rel external
help
license
next
nofollow
noopener
noreferrer
opener
prev
search
Specifies the relationship between a linked resource and the current document
target _blank
_self
_parent
_top
Specifies where to display the response that is received after submitting the form

Thuộc tính toàn cầu

Thẻ <form>cũng hỗ trợ Thuộc tính chung trong HTML .


Thuộc tính sự kiện

Thẻ <form>cũng hỗ trợ Thuộc tính sự kiện trong HTML .



Các ví dụ khác

Thí dụ

Một biểu mẫu HTML với các hộp kiểm:

<form action="/action_page.php" method="get">
  <input type="checkbox" name="vehicle1" value="Bike">
  <label for="vehicle1"> I have a bike</label><br>
  <input type="checkbox" name="vehicle2" value="Car">
  <label for="vehicle2"> I have a car</label><br>
  <input type="checkbox" name="vehicle3" value="Boat" checked>
  <label for="vehicle3"> I have a boat</label><br><br>
  <input type="submit" value="Submit">
</form>

Thí dụ

Một biểu mẫu HTML với các nút radio:

<form action="/action_page.php" method="get">
  <input type="radio" id="html" name="fav_language" value="HTML">
  <label for="html">HTML</label><br>
  <input type="radio" id="css" name="fav_language" value="CSS" checked="checked">
  <label for="css">CSS</label><br>
  <input type="radio" id="javascript" name="fav_language" value="JavaScript">
  <label for="javascript">JavaScript</label><br><br>
  <input type="submit" value="Submit">
</form>

Các trang liên quan

Hướng dẫn HTML: Biểu mẫu và đầu vào HTML

Tham chiếu DOM HTML: Đối tượng Biểu mẫu

Hướng dẫn CSS: Biểu mẫu tạo kiểu


Cài đặt CSS mặc định

Hầu hết các trình duyệt sẽ hiển thị <form>phần tử với các giá trị mặc định sau:

Thí dụ

form {
  display: block;
  margin-top: 0em;
}