Thuộc tính biểu mẫu HTML


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

Thuộc formtính chỉ định hình thức mà phần tử thuộc về.

Giá trị của thuộc tính này phải bằng idthuộc tính của một <form> phần tử trong cùng một tài liệu.


Áp dụng đối với

Thuộc formtính có thể được sử dụng trên các phần tử sau:

Các yếu tố Thuộc tính
<button> hình thức
<fieldset> hình thức
<input> hình thức
<nhãn> hình thức
<meter> hình thức
<đối tượng> hình thức
<đầu ra> hình thức
<select> hình thức
<textareosystem hình thức

Các ví dụ

Ví dụ về nút

Một nút nằm bên ngoài biểu mẫu (nhưng vẫn là một phần của biểu mẫu):

<form action="/action_page.php" method="get" id="form1">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
</form>

<button type="submit" form="form1" value="Submit">Submit</button>

Ví dụ về tập trường

Phần tử <fieldset> nằm bên ngoài biểu mẫu (nhưng vẫn là một phần của biểu mẫu):

<form action="/action_page.php" method="get" id="form1">
  What is your favorite color? <input type="text" name="fav_color"><br>
  <input type="submit">
</form>

<fieldset form="form1">
  Name: <input type="text" name="username"><br>
  Email: <input type="text" name="usermail"><br>
</fieldset>

Ví dụ đầu vào

Trường đầu vào nằm bên ngoài biểu mẫu HTML (nhưng vẫn là một phần của biểu mẫu):

<form action="/action_page.php" id="form1">
  First name: <input type="text" name="fname"><br>
  <input type="submit" value="Submit">
</form>

Last name: <input type="text" name="lname" form="form1">

Ví dụ về nhãn

Phần tử <label> nằm bên ngoài biểu mẫu (nhưng vẫn là một phần của biểu mẫu):

<form action="/action_page.php" id="form1">
  <input type="radio" id="html" name="fav_language" value="HTML"><br>
  <input type="radio" id="css" name="fav_language" value="CSS">
  <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>

<label for="html">HTML</label>

Ví dụ về đồng hồ

Phần tử <meter> nằm bên ngoài biểu mẫu (nhưng vẫn là một phần của biểu mẫu):

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

<meter form="form1" name="x1" min="0" low="40" high="90" max="100" value="95"></meter>

Ví dụ về đối tượng

Phần tử <object> nằm bên ngoài biểu mẫu (nhưng vẫn là một phần của biểu mẫu):

<form action="/action_page.php" id="form1">
  First name: <input type="text" name="fname"><br>
  <input type="submit" value="Submit">
</form>

<object data="helloworld.swf" height="400" width="400" form="form1" name="obj1"></object>

Ví dụ đầu ra

Phần tử <output> nằm bên ngoài biểu mẫu (nhưng vẫn là một phần của biểu mẫu):

<form action="/action_page.php" id="numform"
oninput="x.value=parseInt(a.value)+parseInt(b.value)">0
<input type="range" id="a" name="a" value="50">100
+<input type="number" id="b" name="b" value="50">
<br><br>
<input type="submit">
</form>

<output form="numform" name="x" for="a b"></output>

Chọn ví dụ

Danh sách thả xuống nằm bên ngoài biểu mẫu (nhưng vẫn là một phần của biểu mẫu):

<form action="/action_page.php" id="carform">
  Firstname:<input type="text" name="fname">
  <input type="submit">
</form>

<select name="carlist" form="carform">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

Ví dụ về Textarea

Vùng văn bản nằm bên ngoài biểu mẫu (nhưng vẫn là một phần của biểu mẫu):

<form action="/action_page.php" id="usrform">
  Name: <input type="text" name="usrname">
  <input type="submit">
</form>

<textarea name="comment" form="usrform">Enter text here...</textarea>

Hỗ trợ trình duyệt

Thuộc formtính này có hỗ trợ trình duyệt sau cho từng phần tử:

Element
button 10.0 Not supported 4.0 5.1 9.5
fieldset Not supported Not supported Not supported Not supported Not supported
input 9.0 Not supported 4.0 5.1 10.6
label Yes Yes Yes Yes Yes
meter Not supported Not supported Not supported Not supported Not supported
object Not supported Not supported Not supported Not supported Not supported
output Yes Not supported Yes Yes Yes
select Yes Not supported Yes Yes Yes
textarea Yes Not supported Yes Yes Yes