By GokiSoft.Com| 10:31 05/06/2020|
Tài Liệu HTML

HTML Input form* Attributes - Các thuộc tính form của thành phần input trong HTML


Chương này mô tả các thuộc tính form* khác nhau cho thành phần HTML <input>.




Thuộc tính form


Thuộc tính form chỉ định biểu mẫu mà thành phần  <input> thuộc về.

Giá trị của thuộc tính này phải bằng với thuộc tính id của thành phần <form> mà nó thuộc về.

ví dụ

Một trường input 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">
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <input type="submit" value="Submit">
</form>

<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname" form="form1">
<!DOCTYPE html>
<html>
<body>

<h1>The input form attribute</h1>

<p>The form attribute specifies the form an input element belongs to.</p>

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

<p>The "Last name" field below is outside the form element, but still part of the form.</p>

<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname" form="form1">

</body>
</html>





Thuộc tính formaction


Thuộc formaction chỉ định URL của tệp tin sẽ sử lý input khi biểu mẫu được gửi.

Ghi chú: Thuộc tính này ghi đè thuộc tính  action của thành phần  <form> .

ví dụ

Một biểu mẫu HTML với hai nút gửi, với các hành động khác nhau:

<form action="/action_page.php">
  <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">
  <input type="submit" formaction="/action_page2.php" value="Submit as Admin">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input formaction attribute</h1>

<p>The formaction attribute specifies the URL of a file that will process the input when the form is submitted.</p>

<form action="/action_page.php">
  <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">
  <input type="submit" formaction="/action_page2.php" value="Submit as Admin">
</form>

</body>
</html>





Thuộc tính formenctype


Thuộc tính input formenctype chỉ định cách dữ liệu biểu mẫu nên được mã hóa khi được gửi (duy nhất cho biểu mẫu với phương thức = "post").

Ghi chú: Thuộc tính này ghi đè thuộc tính enctype của thành phần <form> .

Thuộc tính formenctype hoạt động với các kiểu input sau: submit và image.

ví dụ

Một biểu mẫu với hai nút gửi. Nút đầu tiên gửi dữ liệu biểu mẫu với mã hóa mặc định, nút thứ hai gửi dữ liệu biểu mẫu được mã hóa thành "multipart/form-data":

<form action="/action_page_binary.asp" method="post">
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <input type="submit" value="Submit">
  <input type="submit" formenctype="multipart/form-data"
  value="Submit as Multipart/form-data">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input formenctype attribute</h1>

<p>The formenctype attribute specifies how the form data should be encoded when submitted.</p>

<form action="/action_page_binary.asp" method="post">
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <input type="submit" value="Submit">
  <input type="submit" formenctype="multipart/form-data" value="Submit as Multipart/form-data">
</form>

</body>
</html>






Thuộc tính formmethod 


Thuộc tính input formmethod xác định phương thức HTTP để gửi dữ liệu biểu mẫu đến URL hành động.

Ghi chú: Thuộc tính này ghi đề thuộc tính method của thành phần  <form> .

Thuộc tính  formmethod làm việc với cá kiểu input sau: submit và image.

Dữ liệu biểu mẫu có thể được gửi như các biến URL (method="get") hay như một giao thức HTTP post (method="post").

Ghi chú về phương thức "get":

  • Phương thức này nối thêm dữ liệu biểu mẫu vào URL bằng các cặp tên/giá trị
  • Phương thức này hữu dụng cho việc gửi biểu mẫu mà người dùng muốn đánh dấu trang kết quả
  • Có một giới hạn về số lượng dữ liệu mà bạn có thể đặt trong URL (khác nhau giữa các trình duyệt), vì thế, bạn không thể chắc chắn toàn bộ dữ liệu biểu mẫu sẽ được chuyển một cách chính xác
  • Không bao giờ dùng phương thức "get" để chuyển một thông tin nhạy cảm! (mật khẩu hoặc các thông tin nhạy cảm sẽ được hiển thị trong thanh địa chỉ của trình duyệt)

Ghi chú về phương thức "post":

  • Phương thức này gửi các dữ liệu biểu mẫu như một giao thức HTTP post
  • Gửi biểu mẫu với phương thức "post" không thể được đánh dấu trang
  • Phương thức "post" mạnh mẽ và an toàn hơn "get", và "post" không có các giới hạn kích thước


ví dụ

Một biểu mẫu với hai nút gửi. Cái đầu tiên gửi dữ liệu biểu mẫu với phương thức="get". Cái thứ hai gửi dữ liệu biểu mẫu với phương thức="post":

<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 using GET">
  <input type="submit" formmethod="post" value="Submit using POST">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input formmethod Attribute</h1>

<p>The formmethod attribute defines the HTTP method for sending form-data to the action URL.</p>

<form action="/action_page.php" method="get" target="_blank">
  <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 using GET">
  <input type="submit" formmethod="post" value="Submit using POST">
</form>

</body>
</html>





Thuộc tính formtarget 


Thuộc tính input formtarget xác định một tên hoặc từ khóa chỉ ra nơi để hiển thị phản hồi được nhận sau khi gửi biểu mẫu.

Ghi chú: Thuộc tính này ghi đè thuộc tính target của thành phần  <form> .

Thuộc tính formtarget hoạt động với các kiểu input sau: submit và image.

ví dụ

Một biểu mẫu với hai nút gửi, với các cửa sổ target khác nhau:

<form action="/action_page.php">
  <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">
  <input type="submit" formtarget="_blank" value="Submit to a new window/tab">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input formtarget attribute</h1>

<p>The formtarget attribute specifies a name or a keyword that indicates where to display the response that is received after submitting the form.</p>

<form action="/action_page.php">
  <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">
  <input type="submit" formtarget="_blank" value="Submit to a new window/tab">
</form>

</body>
</html>





Thuộc tính formnovalidate 


Thuộc tính formnovalidate chỉ định thành phần <input> không nên được xác thực khi được gửi.

Ghi chú: Thuộc tính này ghi đè thuộc tính novalidate của thành phần <form>.

Thuộc tính  formnovalidate hoạt động với các kiểu input sau: submit.

ví dụ

Một biểu mẫu với hai nút gửi (cùng và không cùng với sự xác thực):

<form action="/action_page.php">
  <label for="email">Enter your email:</label>
  <input type="email" id="email" name="email"><br><br>
  <input type="submit" value="Submit">
  <input type="submit" formnovalidate="formnovalidate"
  value="Submit without validation">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input formnovalidate attribute</h1>

<form action="/action_page.php">
  <label for="email">Enter your email:</label>
  <input type="email" id="email" name="email" required><br><br>
  <input type="submit" value="Submit">
  <input type="submit" formnovalidate="formnovalidate" value="Submit without validation">
</form>

<p><strong>Note:</strong> The formnovalidate attribute of the input tag is not supported in Safari 10 (or earlier).</p>

</body>
</html>





Thuộc tính novalidate


Thuộc tính novalidate là một thuộc tính của  <form> .

Khi xuất hiện, novalidate chỉ định toàn bộ các dữ liệu biểu mẫu không nên được xác thực khi được gửi.

ví dụ

Xác định rằng không dữ liệu biểu mẫu nào nên được xác thực khi gửi:

<form action="/action_page.php" novalidate>
  <label for="email">Enter your email:</label>
  <input type="email" id="email" name="email"><br><br>
  <input type="submit" value="Submit">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The form novalidate attribute</h1>

<p>The novalidate attribute specifies that the form data should not be validated when submitted.</p>

<form action="/action_page.php" novalidate>
  <label for="email">Enter your email:</label>
  <input type="email" id="email" name="email" required><br><br>
  <input type="submit" value="Submit">
</form>

<p><strong>Note:</strong> The novalidate attribute of the form tag is not supported in Safari 10 (or earlier).</p>

</body>
</html>