By GokiSoft.Com| 08:22 06/06/2020|
Tài Liệu HTML

HTML Input Attributes - Các thuộc tính input trong HTML


Chương này mô tả về các thuộc tính khác nhau của thành phần <input> trong HTML.




Thuộc tính value


Thuộc tính value chỉ định một giá trị khởi tạo cho một trường input:

ví dụ

Trường input với các giá trị được khởi tạo(mặc định):

<form>
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" value="John"><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname" value="Doe">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input value attribute</h1>

<p>The value attribute specifies an initial value for an input field:</p>

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

</body>
</html>





Thuộc tính readonly


Thuộc tính readonly chỉ định một trường input chỉ có thể được đọc.

Một trường input chỉ có thể đọc không thể sửa đổi (tuy nhiên, một người dùng có thể tab nó, đánh dấu nó, và sao chép văn bản từ nó).

Giá trị của một trường input chỉ có thể đọc sẽ được gửi khi gửi biểu mẫu!

ví dụ

Một trường input chỉ đọc:

<form>
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" value="John" readonly><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname" value="Doe">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input readonly attribute</h1>

<p>The readonly attribute specifies that an input field should be read-only (cannot be changed):</p>

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

</body>
</html>





Thuộc tính disabled


Thuộc tính input disabled chỉ định một trường input sẽ bị vô hiệu hóa.

Một trường input vô hiệu hóa không sử dụng được và không bấm được.

Giá trị của trường input vô hiệu hóa sẽ không được gửi khi gửi biểu mẫu!

ví dụ

Một trường input vô hiệu hóa:

<form>
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" value="John" disabled><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname" value="Doe">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input disabled attribute</h1>

<p>The disabled attribute specifies that an input field should be disabled (unusable and un-clickable):</p>

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

</body>
</html>





Thuộc tính size


Thuộc tính size chỉ định chiều rộng trực quan, trong các ký tự, của một trường input.

Giá trị mặc định của size là 20.

Ghi chú: Thuộc tính size hoạt động với các kiểu input sau: text, search, tel, email, password.

ví dụ

Đặt một độ rộng cho một trường input:

<form>
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" size="50"><br>
  <label for="pin">PIN:</label><br>
  <input type="text" id="pin" name="pin" size="4">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input size attribute</h1>

<p>The size attribute specifies the width (in characters) of an input field:</p>

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

</body>
</html>





Thuộc tính maxlength


Thuộc tính maxlength chỉ định số lượng tối đa của các ký tự được cho phép trong một trường input.

Ghi chú: Khi một  maxlength được đặt, trường input sẽ không chấp nhận nhiều hơn số lượng của ký tự đã được chỉ định. Tuy nhiên, thuộc tính này không cung cấp bất kỳ phẩn hồi nào. Vì thế, nếu bạn muốn cảnh báo người dùng, bạn phải viết JavaScript code.

ví dụ

Đặt một độ dài tối da cho một trường input:

<form>
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" size="50"><br>
  <label for="pin">PIN:</label><br>
  <input type="text" id="pin" name="pin" maxlength="4" size="4">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input maxlength attribute</h1>

<p>The maxlength attribute specifies the maximum number of characters allowed in an input field:</p>

<form action="/action_page.php">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" size="50"><br>
  <label for="pin">PIN:</label><br>
  <input type="text" id="pin" name="pin" maxlength="4" size="4"><br><br>
  <input type="submit" value="Submit">
</form>

</body>
</html>





Thuộc tính min và max


Thuộc tính min và max chỉ các giá trị tối đa và tối thiểu cho trường input.

Các thuộc tính min và  max hoạt động với các kiểu input sau: number, range, date, datetime-local, month, time và week.

Ghi chú: Sử dụng thuộc tính max và min cùng lúc để tạo một phạm vi các giá trị hợp lệ.

ví dụ

Đặt một max date, một min date, và một phạm vi các giá trị hợp lệ:

<form>
  <label for="datemax">Enter a date before 1980-01-01:</label>
  <input type="date" id="datemax" name="datemax" max="1979-12-31"><br><br>

  <label for="datemin">Enter a date after 2000-01-01:</label>
  <input type="date" id="datemin" name="datemin" min="2000-01-02"><br><br>

  <label for="quantity">Quantity (between 1 and 5):</label>
  <input type="number" id="quantity" name="quantity" min="1" max="5">
</form>>
<!DOCTYPE html>
<html>
<body>

<h1>The input min and max attributes</h1>

<p>The min and max attributes specify the minimum and maximum values for an input element.</p>

<form action="/action_page.php">
  <label for="datemax">Enter a date before 1980-01-01:</label>
  <input type="date" id="datemax" name="datemax" max="1979-12-31"><br><br>

  <label for="datemin">Enter a date after 2000-01-01:</label>
  <input type="date" id="datemin" name="datemin" min="2000-01-02"><br><br>
  
  <label for="quantity">Quantity (between 1 and 5):</label>
  <input type="number" id="quantity" name="quantity" min="1" max="5"><br><br>

  <input type="submit" value="Submit">
</form>

</body>
</html>





Thuộc tính multiple


Thuộc tính multiple chỉ định cho phép người dùng nhập nhiều hơn một giá trị trong một trường input.

Thuộc tính multiple hoạt động với các kiểu input sau: email, và file.

ví dụ

Một trường tải lên tệp tin chấp nhận đa giá trị:

<form>
  <label for="files">Select files:</label>
  <input type="file" id="files" name="files" multiple>
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input multiple attributes</h1>

<p>The multiple attribute specifies that the user is allowed to enter more than one value in an input field.</p>

<form action="/action_page.php">
  <label for="files">Select files:</label>
  <input type="file" id="files" name="files" multiple><br><br>
  <input type="submit" value="Submit">
</form>

<p>To select multiple files, hold down the CTRL or SHIFT key while selecting.</p>

</body>
</html>





Thuộc tính pattern


Thuộc tính pattern chỉ định một biểu thức chính quy mà trường input phải kiểm tra đến, khi một biểu mẫu được gửi.

Thuộc tính  pattern làm việc với các kiểu input sau: text, date, search, url, tel, email, và password.

Ghi chú: Sử dụng các thuộc tính tiêu đề toàn cục mô tả mẫu để giúp người dùng.

Ghi chú: Học nhiều hơn về biểu thức chính quy trong hướng dẫn về JavaScript.

ví dụ

Một trường input chỉ có thể chứa duy nhất 3 chữ cái (không có các chữ số hay các ký tự đặc biệt):

<form>
  <label for="country_code">Country code:</label>
  <input type="text" id="country_code" name="country_code"
  pattern="[A-Za-z]{3}" title="Three letter country code">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input pattern attribute</h1>

<p>The pattern attribute specifies a regular expression that the input element's value is checked against.</p>

<form action="/action_page.php">
  <label for="country_code">Country code:</label>
  <input type="text" id="country_code" name="country_code" pattern="[A-Za-z]{3}" title="Three letter country code"><br><br>
  <input type="submit" value="Submit">
</form>

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

</body>
</html>








Thuộc tính placeholder


Thuộc tính placeholder xác định một gợi ý ngắn mô tả giá trị mong muốn cho một trường input (một giá trị ví dụ hoặc một mô tả ngắn của định dạng mong muốn).

Gợi ý ngắn được hiển thị trong trường input trước khi người dùng nhập vào một giá trị.

Thuộc tính  placeholder hoạt động với các kiểu input sau :  text, search, url, tel, email, và password.

ví dụ

Một trường input với một văn bản giữ chỗ(placeholder):


<form>
  <label for="phone">Enter a phone number:</label>
  <input type="tel" id="phone" name="phone"
  placeholder="123-45-678"
  pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input placeholder attribute</h1>

<p>The placeholder attribute specifies a short hint that describes the expected value of an input field.</p>

<form action="/action_page.php">
  <label for="phone">Enter a phone number:</label>
  <input type="tel" id="phone" name="phone" placeholder="123-45-678" pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}"><br><br>
  <input type="submit" value="Submit">
</form>

</body>
</html>





Thuộc tính required


Thuộc tính input required chỉ định một trường input phải được điền vào trước khi gửi biểu mẫu.

Thuộc tính required hoạt với các kiểu input sau: text, search, url, tel, email, password, date pickers, number, checkbox, radio, và file.

ví dụ

Một trường input bắt buộc:

<form>
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" required>
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input required attribute</h1>

<p>The required attribute specifies that an input field must be filled out before submitting the form.</p>

<form action="/action_page.php">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" required>
  <input type="submit" value="Submit">
</form>

<p><strong>Note:</strong> The required attribute of the input tag is not supported in Safari prior version 10.1.</p>

</body>
</html>





Thuộc tính step


Thuộc tính step chỉ định các khoảng số hợp lệ cho một trường input.

Ví dụ: nếu step="3", các số hợp lệ có thể là -3, 0, 3 , 6, etc.

Ghi chú: Thuộc tính này có thể được sử dụng cùng với các thuộc tính max và min để tạo ra một phạm vi của các giá trị hợp lệ.

Thuộc tính step hoạt động với các kiểu input sau: number, range, date, datetime-local, month, time và week.

ví dụ

Một trường input với các khoảng số hợp lệ:

<form>
  <label for="points">Points:</label>
  <input type="number" id="points" name="points" step="3">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input step attribute</h1>

<p>The step attribute specifies the legal number intervals for an input element.</p>

<form action="/action_page.php">
  <label for="points">Points:</label>
  <input type="number" id="points" name="points" step="3">
  <input type="submit" value="Submit">
</form>

</body>
</html>


Ghi chú: Các hạn chế đầu vào không hoàn hảo, và JavaScript cung cấp rất nhiều cách để thêm các input không hợp lệ. Để hạn chế đầu vào một cách an toàn, nó cũng phải được kiểm tra bởi đầu nhận (máy chủ)!





Thuộc tính autofocus


Thuộc tính input autofocus chỉ định một trường input nên tập được tập trung vào một cách tự động khi trang được tải.

ví dụ

Để trường input "First Name" được tập trung một cách tự động khi tải trang:

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

<h1>The input autofocus attribute</h1>

<p>The autofocus attribute specifies that the input field should automatically get focus when the page loads.</p>

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

</body>
</html>






Thuộc tính height và width


Thuộc tính input height và width chỉ định chiều cao và chiều rộng của một thành phần <input type="image"> .

Mẹo: Luôn chỉ định cả hai thuộc tính chiều cao và chiều rộng cho các hình ảnh. Nếu chiều cao và chiều rộng được đặt, không gian bắt buộc cho hình ảnh sẽ được giữ khi trang được tải. Khi không có các thuộc tính đó, trình duyệt không biết kích thước của hình ảnh, và không thể giữ không gian thích hợp cho nó. Hiệu ứng sẽ là bố cục trang sẽ thay đổi trong khi tải (trong khi tải hình ảnh).

ví dụ

Xác định một hình ảnh như một nút gửi, với các thuộc tính chiều cao và chiều rộng:

<form>
  <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="image" src="img_submit.gif" alt="Submit" width="48" height="48">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input height and width attributes</h1>

<p>The height and width attributes specify the height and width of an input type="image" element.</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="image" src="img_submit.gif" alt="Submit" width="48" height="48">
</form>

<p><b>Note:</b> The input type="image" sends the X and Y coordinates of the click that activated the image button.</p>

</body>
</html>






Thuộc tính list


Thuộc tính input list tham chiếu đến một thành phần  <datalist> chứa các tùy chọn được xác định trước cho một thành phần input.

ví dụ

Một thành phần <input> với các giá trị được xác định trước trong một <datalist>:

<form>
  <input list="browsers">
  <datalist id="browsers">
    <option value="Internet Explorer">
    <option value="Firefox">
    <option value="Chrome">
    <option value="Opera">
    <option value="Safari">
  </datalist>
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The input list attribute</h1>

<p>The list attribute refers to a datalist element that contains pre-defined options for an input element.</p>

<form action="/action_page.php">
  <input list="browsers" name="browser">
  <datalist id="browsers">
    <option value="Internet Explorer">
    <option value="Firefox">
    <option value="Chrome">
    <option value="Opera">
    <option value="Safari">
  </datalist>
  <input type="submit" value="Submit">
</form>

<p><b>Note:</b> The datalist tag is not supported in Safari 12.0 (or earlier).</p>

</body>
</html>





Thuộc tính autocomplete


Thuộc tính autocomplete xác định khi nào một biểu mẫu hoặc một trường input nên có tự động điền bật hoặc tắt.

Tự động điền cho phép trình duyệt tiên đoán giá trị. Kho người dùng bắt đầu nhập vào một trường, trình duyệt nên hiển thị các tùy chọn để điển vào trường, dựa trên các giá trị đã được nhập trước đó.

Thuộc tính autocomplete hoạt động với  <form> và các kiểu  <input> sau: text, search, url, tel, email, password, datepickers, range, và color.

ví dụ

Một biểu mẫu HTML với tự động điền được bật, và tắt cho một trường input:

<form action="/action_page.php" autocomplete="on">
  <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>
  <label for="email">Email:</label>
  <input type="email" id="email" name="email" autocomplete="off"><br><br>
  <input type="submit" value="Submit">
</form>
<!DOCTYPE html>
<html>
<body>

<h1>The autocomplete attribute</h1>

<p>The autocomplete attribute specifies whether or not an input field should have autocomplete enabled.</p>

<p>Fill in and submit the form, then reload the page to see how autocomplete works.</p>

<p>Notice that autocomplete is "on" for the form, but "off" for the e-mail field!</p>

<form action="/action_page.php" autocomplete="on">
  <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>
  <label for="email">Email:</label>
  <input type="email" id="email" name="email" autocomplete="off"><br><br>
  <input type="submit" value="Submit">
</form>

</body>
</html>


Ghi chú: TRong một số trình duyệt bạn có thể phải kích hoạt chức năng tự động điền để nó hoạt động (Nhìn dưới "Preferences" trong menu trình duyệt).