Hướng dẫn is month input type attribute in html5? - thuộc tính loại đầu vào tháng trong html5?

<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
2 Các yếu tố của loại
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3 Tạo các trường đầu vào cho phép người dùng nhập một tháng và năm cho phép một tháng và năm dễ dàng nhập. Giá trị là một chuỗi có giá trị ở định dạng "
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
4", trong đó
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
5 là năm bốn chữ số và
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
6 là số tháng.
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3
create input fields that let the user enter a month and year allowing a month and year to be easily entered. The value is a string whose value is in the format "
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
4", where
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
5 is the four-digit year and
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
6 is the month number.

Thử nó

UI của điều khiển nói chung từ trình duyệt đến trình duyệt; Tại thời điểm hỗ trợ là chắp vá, chỉ có Chrome/Opera và Edge trên máy tính để bàn - và hầu hết các phiên bản trình duyệt di động hiện đại - có các triển khai có thể sử dụng. Trong các trình duyệt không hỗ trợ đầu vào

<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3, điều khiển giảm dần một cách duyên dáng thành một
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
8 đơn giản, mặc dù có thể có xác thực tự động của văn bản đã nhập để đảm bảo nó được định dạng như mong đợi.

Đối với những người bạn sử dụng trình duyệt không hỗ trợ

<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3, ảnh chụp màn hình bên dưới cho thấy nó trông như thế nào trong Chrome và Opera. Nhấp vào mũi tên xuống ở phía bên phải mang đến một người chọn ngày cho phép bạn chọn tháng và năm.

Hướng dẫn is month input type attribute in html5? - thuộc tính loại đầu vào tháng trong html5?

Điều khiển Microsoft Edge

<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3 trông như thế này:

Hướng dẫn is month input type attribute in html5? - thuộc tính loại đầu vào tháng trong html5?

Giá trị

Một chuỗi đại diện cho giá trị của tháng và năm được nhập vào đầu vào, dưới dạng yyyy-mm (năm hoặc nhiều chữ số, sau đó là dấu gạch nối ("

const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
1"), sau đó là tháng hai chữ số). Định dạng của chuỗi tháng được sử dụng bởi loại đầu vào này được mô tả ở định dạng của chuỗi tháng cục bộ hợp lệ theo định dạng ngày và thời gian được sử dụng trong HTML.

Đặt giá trị mặc định

Bạn có thể đặt giá trị mặc định cho điều khiển đầu vào bằng cách bao gồm một tháng và năm bên trong thuộc tính

const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
2, như vậy:

<label for="bday-month">What month were you born in?label>
 <input id="bday-month" type="month" name="bday-month" value="2001-06" />

Một điều cần lưu ý là định dạng ngày được hiển thị khác với

const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
2 thực tế; Hầu hết các đại lý người dùng hiển thị tháng và năm dưới dạng phù hợp với địa phương, dựa trên địa phương đặt của hệ điều hành của người dùng, trong khi ngày
const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
2 luôn được định dạng
const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
5.

Ví dụ, khi giá trị trên được gửi đến máy chủ, nó sẽ trông giống như

const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
6.

Đặt giá trị bằng JavaScript

Bạn cũng có thể nhận và đặt giá trị ngày trong JavaScript bằng thuộc tính

const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
7, ví dụ:

<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />

const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';

Thuộc tính bổ sung

Ngoài các thuộc tính phổ biến cho các phần tử

<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
2, đầu vào tháng cung cấp các thuộc tính sau.

danh sách

Các giá trị của thuộc tính danh sách là

const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
9 của phần tử
<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
0 nằm trong cùng một tài liệu.
<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
0 cung cấp một danh sách các giá trị được xác định trước để đề xuất cho người dùng cho đầu vào này. Bất kỳ giá trị nào trong danh sách không tương thích với
<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
2 không được bao gồm trong các tùy chọn được đề xuất. Các giá trị được cung cấp là các đề xuất, không phải yêu cầu: Người dùng có thể chọn từ danh sách được xác định trước này hoặc cung cấp một giá trị khác.

Tối đa

Năm mới nhất và tháng, ở định dạng chuỗi được thảo luận trong phần giá trị ở trên, để chấp nhận. Nếu

const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
2 nhập vào phần tử vượt quá điều này, phần tử sẽ không xác thực ràng buộc. Nếu giá trị của thuộc tính
<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
4 không phải là một chuỗi hợp lệ ở định dạng "
const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
5", thì phần tử không có giá trị tối đa.

Giá trị này phải chỉ định một cặp một tháng một lần muộn hơn hoặc bằng với giá trị được chỉ định bởi thuộc tính

<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
6.

Tối thiểu

Năm mới nhất và tháng để chấp nhận, trong cùng định dạng "

const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
5" được mô tả ở trên. Nếu
const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
2 của phần tử nhỏ hơn thế này, phần tử sẽ không xác thực ràng buộc. Nếu một giá trị được chỉ định cho
<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
6 không phải là chuỗi năm và tháng hợp lệ, đầu vào không có giá trị tối thiểu.

Giá trị này phải là một cặp một tháng, sớm hơn hoặc bằng với giá trị được chỉ định bởi thuộc tính

<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
4.

chỉ đọc

Một thuộc tính Boolean, nếu có, có nghĩa là trường này không thể được chỉnh sửa bởi người dùng. Tuy nhiên,

const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
2 của nó vẫn có thể được thay đổi từ mã JavaScript trực tiếp đặt giá trị của thuộc tính
const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
7.

Lưu ý: Vì trường chỉ đọc không thể có giá trị,

<form>
  <label for="bday-month">What month were you born in?label>
  <input
    id="bday-month"
    type="month"
    name="bday-month"
    min="1900-01"
    max="2013-12" />
form>
3 không có bất kỳ ảnh hưởng nào đến đầu vào với thuộc tính
<form>
  <label for="bday-month">What month were you born in?label>
  <input
    id="bday-month"
    type="month"
    name="bday-month"
    min="1900-01"
    max="2013-12" />
form>
4 cũng được chỉ định.
Because a read-only field cannot have a value,
<form>
  <label for="bday-month">What month were you born in?label>
  <input
    id="bday-month"
    type="month"
    name="bday-month"
    min="1900-01"
    max="2013-12" />
form>
3 does not have any effect on inputs with the
<form>
  <label for="bday-month">What month were you born in?label>
  <input
    id="bday-month"
    type="month"
    name="bday-month"
    min="1900-01"
    max="2013-12" />
form>
4 attribute also specified.

bươc

Thuộc tính

<form>
  <label for="bday-month">What month were you born in?label>
  <input
    id="bday-month"
    type="month"
    name="bday-month"
    min="1900-01"
    max="2013-12" />
form>
5 là một số chỉ định độ chi tiết mà giá trị phải tuân thủ hoặc giá trị đặc biệt
<form>
  <label for="bday-month">What month were you born in?label>
  <input
    id="bday-month"
    type="month"
    name="bday-month"
    min="1900-01"
    max="2013-12" />
form>
6, được mô tả dưới đây. Chỉ các giá trị bằng với cơ sở để bước (
<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
6 nếu được chỉ định,
const monthControl = document.querySelector('input[type="month"]');
monthControl.value = '2001-06';
2 nếu không và giá trị mặc định phù hợp nếu không được cung cấp) là hợp lệ.

Giá trị chuỗi của

<form>
  <label for="bday-month">What month were you born in?label>
  <input
    id="bday-month"
    type="month"
    name="bday-month"
    min="1900-01"
    max="2013-12" />
form>
6 có nghĩa là không có bước nào được ngụ ý và bất kỳ giá trị nào được cho phép (chặn các ràng buộc khác, chẳng hạn như
<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
6 và
<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
4).

Lưu ý: Khi dữ liệu được người dùng nhập không tuân thủ cấu hình bước, tác nhân người dùng có thể làm tròn đến giá trị hợp lệ gần nhất, thích các số theo hướng dương khi có hai tùy chọn gần nhau. When the data entered by the user doesn't adhere to the stepping configuration, the user agent may round to the nearest valid value, preferring numbers in the positive direction when there are two equally close options.

Đối với đầu vào

<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3, giá trị của
<form>
  <label for="bday-month">What month were you born in?label>
  <input
    id="bday-month"
    type="month"
    name="bday-month"
    min="1900-01"
    max="2013-12" />
form>
5 được đưa ra trong nhiều tháng, với hệ số tỷ lệ là 1 (vì giá trị số cơ bản cũng là trong tháng). Giá trị mặc định của
<form>
  <label for="bday-month">What month were you born in?label>
  <input
    id="bday-month"
    type="month"
    name="bday-month"
    min="1900-01"
    max="2013-12" />
form>
5 là 1 tháng.

Sử dụng đầu vào tháng

Đầu vào liên quan đến ngày (bao gồm

<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3) âm thanh thuận tiện ngay từ cái nhìn đầu tiên; Họ hứa hẹn một giao diện người dùng dễ dàng để chọn ngày và họ bình thường hóa định dạng dữ liệu được gửi đến máy chủ, bất kể địa phương của người dùng. Tuy nhiên, có những vấn đề với
<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
6 bởi vì tại thời điểm này, nhiều trình duyệt lớn chưa hỗ trợ nó.

Chúng tôi sẽ xem xét các cách sử dụng cơ bản và phức tạp hơn của

<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
6, sau đó đưa ra lời khuyên về việc giảm thiểu vấn đề hỗ trợ trình duyệt trong phần hỗ trợ trình duyệt xử lý phần).

Sử dụng cơ bản của tháng

Việc sử dụng đơn giản nhất

<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
6 liên quan đến sự kết hợp phần tử
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
2 và
div {
  margin-bottom: 10px;
  position: relative;
}

input[type="number"] {
  width: 100px;
}

input + span {
  padding-right: 30px;
}

input:invalid + span::after {
  position: absolute;
  content: "✖";
  padding-left: 5px;
}

input:valid + span::after {
  position: absolute;
  content: "✓";
  padding-left: 5px;
}
0 cơ bản, như đã thấy dưới đây:

<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>

Đặt ngày tối đa và tối thiểu

Bạn có thể sử dụng các thuộc tính

<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
6 và
<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
4 để hạn chế phạm vi ngày mà người dùng có thể chọn. Trong ví dụ sau, chúng tôi chỉ định một tháng tối thiểu là
div {
  margin-bottom: 10px;
  position: relative;
}

input[type="number"] {
  width: 100px;
}

input + span {
  padding-right: 30px;
}

input:invalid + span::after {
  position: absolute;
  content: "✖";
  padding-left: 5px;
}

input:valid + span::after {
  position: absolute;
  content: "✓";
  padding-left: 5px;
}
3 và tháng tối đa là
div {
  margin-bottom: 10px;
  position: relative;
}

input[type="number"] {
  width: 100px;
}

input + span {
  padding-right: 30px;
}

input:invalid + span::after {
  position: absolute;
  content: "✖";
  padding-left: 5px;
}

input:valid + span::after {
  position: absolute;
  content: "✓";
  padding-left: 5px;
}
4:

<form>
  <label for="bday-month">What month were you born in?label>
  <input
    id="bday-month"
    type="month"
    name="bday-month"
    min="1900-01"
    max="2013-12" />
form>

Kết quả ở đây là:

  • Chỉ vài tháng từ tháng 1 năm 1900 đến tháng 12 năm 2013 có thể được chọn; Một tháng bên ngoài phạm vi đó không thể được cuộn đến trong điều khiển.
  • Tùy thuộc vào trình duyệt bạn đang sử dụng, bạn có thể thấy rằng nhiều tháng ngoài phạm vi được chỉ định có thể không thể chọn trong trình chọn tháng (ví dụ: cạnh) hoặc không hợp lệ (xem xác thực) nhưng vẫn có sẵn (ví dụ: chrome).

Kiểm soát kích thước đầu vào

<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
6 không hỗ trợ các thuộc tính kích thước biểu mẫu như
div {
  margin-bottom: 10px;
  position: relative;
}

input[type="number"] {
  width: 100px;
}

input + span {
  padding-right: 30px;
}

input:invalid + span::after {
  position: absolute;
  content: "✖";
  padding-left: 5px;
}

input:valid + span::after {
  position: absolute;
  content: "✓";
  padding-left: 5px;
}
6. Bạn sẽ phải dùng đến CSS cho nhu cầu định cỡ.

Thẩm định

Theo mặc định,

<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
6 không áp dụng bất kỳ xác thực nào cho các giá trị đã nhập. Việc triển khai UI thường không cho phép bạn nhập bất cứ thứ gì không phải là ngày - rất hữu ích - nhưng bạn vẫn có thể gửi biểu mẫu với đầu vào
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3 trống hoặc nhập một ngày không hợp lệ (ví dụ: thứ 32 của tháng Tư).

Để giúp tránh điều này, bạn có thể sử dụng

<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
6 và
<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
4 để hạn chế các ngày có sẵn (xem Cài đặt ngày tối đa và tối thiểu) và ngoài ra, sử dụng thuộc tính
<form>
  <label for="bday-month">What month were you born in?label>
  <input
    id="bday-month"
    type="month"
    name="bday-month"
    min="1900-01"
    max="2013-12" />
form>
3 để thực hiện điền vào ngày bắt buộc. Do đó, các trình duyệt hỗ trợ sẽ hiển thị lỗi nếu bạn cố gắng gửi một ngày nằm ngoài giới hạn bộ hoặc trường ngày trống.

Hãy xem xét một ví dụ; Ở đây chúng tôi đã đặt ngày tối thiểu và tối đa, và cũng được yêu cầu trường:

<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>

Nếu bạn cố gắng gửi biểu mẫu mà không có cả tháng và năm được chỉ định (hoặc với một ngày bên ngoài giới hạn đã đặt), trình duyệt sẽ hiển thị lỗi. Hãy thử chơi với ví dụ ngay bây giờ:

Đây là ảnh chụp màn hình cho những người không sử dụng trình duyệt hỗ trợ:

Hướng dẫn is month input type attribute in html5? - thuộc tính loại đầu vào tháng trong html5?

Đây là CSS được sử dụng trong ví dụ trên. Ở đây chúng tôi sử dụng các thuộc tính CSS

<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required
      pattern="[0-9]{4}-[0-9]{2}" />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
2 và
<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required
      pattern="[0-9]{4}-[0-9]{2}" />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
3 để tạo kiểu đầu vào dựa trên việc giá trị hiện tại có hợp lệ hay không. Chúng tôi đã phải đặt các biểu tượng trên
<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required
      pattern="[0-9]{4}-[0-9]{2}" />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
4 bên cạnh đầu vào, không phải trên chính đầu vào, bởi vì trong Chrome, nội dung được tạo được đặt bên trong điều khiển biểu mẫu và không thể được tạo kiểu hoặc hiển thị hiệu quả.

div {
  margin-bottom: 10px;
  position: relative;
}

input[type="number"] {
  width: 100px;
}

input + span {
  padding-right: 30px;
}

input:invalid + span::after {
  position: absolute;
  content: "✖";
  padding-left: 5px;
}

input:valid + span::after {
  position: absolute;
  content: "✓";
  padding-left: 5px;
}

Cảnh báo: Xác thực biểu mẫu HTML không phải là một thay thế cho các tập lệnh đảm bảo rằng dữ liệu đã nhập ở định dạng thích hợp. Thật dễ dàng để ai đó thực hiện các điều chỉnh cho HTML cho phép họ bỏ qua việc xác thực hoặc loại bỏ nó hoàn toàn. Ai đó cũng có thể bỏ qua hoàn toàn HTML của bạn và gửi dữ liệu trực tiếp đến máy chủ của bạn. Nếu mã phía máy chủ của bạn không xác thực dữ liệu mà nó nhận được, thảm họa có thể tấn công khi dữ liệu được định dạng không đúng cách được gửi (hoặc dữ liệu quá lớn, loại sai, v.v.). HTML form validation is not a substitute for scripts that ensure that the entered data is in the proper format. It's far too easy for someone to make adjustments to the HTML that allow them to bypass the validation, or to remove it entirely. It's also possible for someone to bypass your HTML entirely and submit the data directly to your server. If your server-side code fails to validate the data it receives, disaster could strike when improperly-formatted data is submitted (or data which is too large, of the wrong type, and so forth).

Xử lý hỗ trợ trình duyệt

Như đã đề cập ở trên, vấn đề chính với việc sử dụng đầu vào ngày tại thời điểm viết là nhiều trình duyệt chính chưa thực hiện tất cả; Chỉ có Chrome/Opera và Edge hỗ trợ nó trên máy tính để bàn và hầu hết các trình duyệt hiện đại trên thiết bị di động. Ví dụ, trình chọn

<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3 trên Chrome cho Android trông như thế này:

Hướng dẫn is month input type attribute in html5? - thuộc tính loại đầu vào tháng trong html5?

Các trình duyệt không hỗ trợ xuống cấp duyên dáng với đầu vào văn bản, nhưng điều này tạo ra các vấn đề cả về tính nhất quán của giao diện người dùng (điều khiển được trình bày sẽ khác nhau) và xử lý dữ liệu.

Vấn đề thứ hai là nghiêm trọng hơn trong hai người. Như đã đề cập trước đó, với đầu vào

<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3, giá trị thực tế luôn được chuẩn hóa thành định dạng
<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required
      pattern="[0-9]{4}-[0-9]{2}" />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
7. Mặt khác, trong cấu hình mặc định của nó, đầu vào
<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required
      pattern="[0-9]{4}-[0-9]{2}" />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
8 không biết ngày nào định dạng ngày nào và đây là một vấn đề vì số lượng các cách khác nhau mà mọi người viết ngày. Ví dụ:

  • <form>
      <div>
        <label for="month">
          What month would you like to visit (June to Sept.)?
        label>
        <input
          id="month"
          type="month"
          name="month"
          min="2022-06"
          max="2022-09"
          required
          pattern="[0-9]{4}-[0-9]{2}" />
        <span class="validity">span>
      div>
      <div>
        <input type="submit" value="Submit form" />
      div>
    form>
    
    9 (072022)
  • div {
      margin-bottom: 10px;
      position: relative;
    }
    
    input[type="number"] {
      width: 100px;
    }
    
    input + span {
      padding-right: 30px;
    }
    
    input:invalid + span::after {
      position: absolute;
      content: "✖";
      padding-left: 5px;
    }
    
    input:valid + span::after {
      position: absolute;
      content: "✓";
      padding-left: 5px;
    }
    
    0 (07/2022)
  • div {
      margin-bottom: 10px;
      position: relative;
    }
    
    input[type="number"] {
      width: 100px;
    }
    
    input + span {
      padding-right: 30px;
    }
    
    input:invalid + span::after {
      position: absolute;
      content: "✖";
      padding-left: 5px;
    }
    
    input:valid + span::after {
      position: absolute;
      content: "✓";
      padding-left: 5px;
    }
    
    1 (07-2022)
  • <form>
      <div>
        <label for="month">
          What month would you like to visit (June to Sept.)?
        label>
        <input
          id="month"
          type="month"
          name="month"
          min="2022-06"
          max="2022-09"
          required
          pattern="[0-9]{4}-[0-9]{2}" />
        <span class="validity">span>
      div>
      <div>
        <input type="submit" value="Submit form" />
      div>
    form>
    
    7 (2022-07)
  • div {
      margin-bottom: 10px;
      position: relative;
    }
    
    input[type="number"] {
      width: 100px;
    }
    
    input + span {
      padding-right: 30px;
    }
    
    input:invalid + span::after {
      position: absolute;
      content: "✖";
      padding-left: 5px;
    }
    
    input:valid + span::after {
      position: absolute;
      content: "✓";
      padding-left: 5px;
    }
    
    3 (tháng 7 năm 2022)
  • và kể từ đó trở đi…

Một cách xung quanh này là đặt một thuộc tính

div {
  margin-bottom: 10px;
  position: relative;
}

input[type="number"] {
  width: 100px;
}

input + span {
  padding-right: 30px;
}

input:invalid + span::after {
  position: absolute;
  content: "✖";
  padding-left: 5px;
}

input:valid + span::after {
  position: absolute;
  content: "✓";
  padding-left: 5px;
}
4 trên đầu vào
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3 của bạn. Mặc dù đầu vào
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3 không sử dụng nó, nếu trình duyệt rơi trở lại để xử lý nó giống như đầu vào
<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required
      pattern="[0-9]{4}-[0-9]{2}" />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
8, mẫu sẽ được sử dụng. Ví dụ: hãy thử xem bản demo sau trong trình duyệt không hỗ trợ đầu vào
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3:

<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required
      pattern="[0-9]{4}-[0-9]{2}" />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>

Nếu bạn thử gửi nó, bạn sẽ thấy rằng trình duyệt hiện hiển thị thông báo lỗi (và làm nổi bật đầu vào là không hợp lệ) nếu mục nhập của bạn không khớp với mẫu

div {
  margin-bottom: 10px;
  position: relative;
}

input[type="number"] {
  width: 100px;
}

input + span {
  padding-right: 30px;
}

input:invalid + span::after {
  position: absolute;
  content: "✖";
  padding-left: 5px;
}

input:valid + span::after {
  position: absolute;
  content: "✓";
  padding-left: 5px;
}
9, trong đó
<form>
  <div class="nativeDatePicker">
    <label for="month-visit">What month would you like to visit us?label>
    <input type="month" id="month-visit" name="month-visit" />
    <span class="validity">span>
  div>
  <p class="fallbackLabel">What month would you like to visit us?p>
  <div class="fallbackDatePicker">
    <div>
      <span>
        <label for="month">Month:label>
        <select id="month" name="month">
          <option selected>Januaryoption>
          <option>Februaryoption>
          <option>Marchoption>
          <option>Apriloption>
          <option>Mayoption>
          <option>Juneoption>
          <option>Julyoption>
          <option>Augustoption>
          <option>Septemberoption>
          <option>Octoberoption>
          <option>Novemberoption>
          <option>Decemberoption>
        select>
      span>
      <span>
        <label for="year">Year:label>
        <select id="year" name="year">select>
      span>
    div>
  div>
form>
0 là số từ 0 đến 9. Tất nhiên, tất nhiên, Điều này không ngăn mọi người nhập ngày không hợp lệ (chẳng hạn như
<form>
  <div class="nativeDatePicker">
    <label for="month-visit">What month would you like to visit us?label>
    <input type="month" id="month-visit" name="month-visit" />
    <span class="validity">span>
  div>
  <p class="fallbackLabel">What month would you like to visit us?p>
  <div class="fallbackDatePicker">
    <div>
      <span>
        <label for="month">Month:label>
        <select id="month" name="month">
          <option selected>Januaryoption>
          <option>Februaryoption>
          <option>Marchoption>
          <option>Apriloption>
          <option>Mayoption>
          <option>Juneoption>
          <option>Julyoption>
          <option>Augustoption>
          <option>Septemberoption>
          <option>Octoberoption>
          <option>Novemberoption>
          <option>Decemberoption>
        select>
      span>
      <span>
        <label for="year">Year:label>
        <select id="year" name="year">select>
      span>
    div>
  div>
form>
1) hoặc ngày được định dạng không chính xác theo mẫu.

Cũng có vấn đề mà người dùng sẽ không nhất thiết phải biết định dạng ngày nào trong số nhiều ngày được mong đợi. Chúng tôi còn lại việc phải làm.

div {
  margin-bottom: 10px;
  position: relative;
}

input[type="number"] {
  width: 100px;
}

input + span {
  padding-right: 30px;
}

input:invalid + span::after {
  position: absolute;
  content: "✖";
  padding-left: 5px;
}

input:valid + span::after {
  position: absolute;
  content: "✓";
  padding-left: 5px;
}

Cách tốt nhất để đối phó với ngày trong các hình thức theo cách trình duyệt chéo (cho đến khi tất cả các trình duyệt chính đã hỗ trợ chúng trong một thời gian) là để người dùng nhập tháng và năm trong các điều khiển riêng biệt (các yếu tố ____92 đang phổ biến; xem bên dưới để thực hiện) hoặc sử dụng các thư viện JavaScript như plugin Picker JQuery Date.

Ví dụ

Trong ví dụ này, chúng tôi tạo hai bộ phần tử UI, mỗi bộ được thiết kế để cho phép người dùng chọn một tháng và năm. Đầu tiên là đầu vào

<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3 gốc và cái còn lại là một cặp phần tử
<form>
  <div class="nativeDatePicker">
    <label for="month-visit">What month would you like to visit us?label>
    <input type="month" id="month-visit" name="month-visit" />
    <span class="validity">span>
  div>
  <p class="fallbackLabel">What month would you like to visit us?p>
  <div class="fallbackDatePicker">
    <div>
      <span>
        <label for="month">Month:label>
        <select id="month" name="month">
          <option selected>Januaryoption>
          <option>Februaryoption>
          <option>Marchoption>
          <option>Apriloption>
          <option>Mayoption>
          <option>Juneoption>
          <option>Julyoption>
          <option>Augustoption>
          <option>Septemberoption>
          <option>Octoberoption>
          <option>Novemberoption>
          <option>Decemberoption>
        select>
      span>
      <span>
        <label for="year">Year:label>
        <select id="year" name="year">select>
      span>
    div>
  div>
form>
2 cho phép chọn một tháng và năm một cách độc lập, để tương thích với các trình duyệt chưa hỗ trợ
<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
6.

HTML

Mẫu yêu cầu tháng và năm trông như thế này:

<form>
  <div class="nativeDatePicker">
    <label for="month-visit">What month would you like to visit us?label>
    <input type="month" id="month-visit" name="month-visit" />
    <span class="validity">span>
  div>
  <p class="fallbackLabel">What month would you like to visit us?p>
  <div class="fallbackDatePicker">
    <div>
      <span>
        <label for="month">Month:label>
        <select id="month" name="month">
          <option selected>Januaryoption>
          <option>Februaryoption>
          <option>Marchoption>
          <option>Apriloption>
          <option>Mayoption>
          <option>Juneoption>
          <option>Julyoption>
          <option>Augustoption>
          <option>Septemberoption>
          <option>Octoberoption>
          <option>Novemberoption>
          <option>Decemberoption>
        select>
      span>
      <span>
        <label for="year">Year:label>
        <select id="year" name="year">select>
      span>
    div>
  div>
form>

<form>
  <div class="nativeDatePicker">
    <label for="month-visit">What month would you like to visit us?label>
    <input type="month" id="month-visit" name="month-visit" />
    <span class="validity">span>
  div>
  <p class="fallbackLabel">What month would you like to visit us?p>
  <div class="fallbackDatePicker">
    <div>
      <span>
        <label for="month">Month:label>
        <select id="month" name="month">
          <option selected>Januaryoption>
          <option>Februaryoption>
          <option>Marchoption>
          <option>Apriloption>
          <option>Mayoption>
          <option>Juneoption>
          <option>Julyoption>
          <option>Augustoption>
          <option>Septemberoption>
          <option>Octoberoption>
          <option>Novemberoption>
          <option>Decemberoption>
        select>
      span>
      <span>
        <label for="year">Year:label>
        <select id="year" name="year">select>
      span>
    div>
  div>
form>
6 với ID
<form>
  <div class="nativeDatePicker">
    <label for="month-visit">What month would you like to visit us?label>
    <input type="month" id="month-visit" name="month-visit" />
    <span class="validity">span>
  div>
  <p class="fallbackLabel">What month would you like to visit us?p>
  <div class="fallbackDatePicker">
    <div>
      <span>
        <label for="month">Month:label>
        <select id="month" name="month">
          <option selected>Januaryoption>
          <option>Februaryoption>
          <option>Marchoption>
          <option>Apriloption>
          <option>Mayoption>
          <option>Juneoption>
          <option>Julyoption>
          <option>Augustoption>
          <option>Septemberoption>
          <option>Octoberoption>
          <option>Novemberoption>
          <option>Decemberoption>
        select>
      span>
      <span>
        <label for="year">Year:label>
        <select id="year" name="year">select>
      span>
    div>
  div>
form>
7 sử dụng loại đầu vào
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3 để yêu cầu tháng và năm, trong khi
<form>
  <div class="nativeDatePicker">
    <label for="month-visit">What month would you like to visit us?label>
    <input type="month" id="month-visit" name="month-visit" />
    <span class="validity">span>
  div>
  <p class="fallbackLabel">What month would you like to visit us?p>
  <div class="fallbackDatePicker">
    <div>
      <span>
        <label for="month">Month:label>
        <select id="month" name="month">
          <option selected>Januaryoption>
          <option>Februaryoption>
          <option>Marchoption>
          <option>Apriloption>
          <option>Mayoption>
          <option>Juneoption>
          <option>Julyoption>
          <option>Augustoption>
          <option>Septemberoption>
          <option>Octoberoption>
          <option>Novemberoption>
          <option>Decemberoption>
        select>
      span>
      <span>
        <label for="year">Year:label>
        <select id="year" name="year">select>
      span>
    div>
  div>
form>
6 với ID
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
00 thay vào đó sử dụng một cặp phần tử
<form>
  <div class="nativeDatePicker">
    <label for="month-visit">What month would you like to visit us?label>
    <input type="month" id="month-visit" name="month-visit" />
    <span class="validity">span>
  div>
  <p class="fallbackLabel">What month would you like to visit us?p>
  <div class="fallbackDatePicker">
    <div>
      <span>
        <label for="month">Month:label>
        <select id="month" name="month">
          <option selected>Januaryoption>
          <option>Februaryoption>
          <option>Marchoption>
          <option>Apriloption>
          <option>Mayoption>
          <option>Juneoption>
          <option>Julyoption>
          <option>Augustoption>
          <option>Septemberoption>
          <option>Octoberoption>
          <option>Novemberoption>
          <option>Decemberoption>
        select>
      span>
      <span>
        <label for="year">Year:label>
        <select id="year" name="year">select>
      span>
    div>
  div>
form>
2. Yêu cầu đầu tiên vào tháng, và năm thứ hai.

<form>
  <div class="nativeDatePicker">
    <label for="month-visit">What month would you like to visit us?label>
    <input type="month" id="month-visit" name="month-visit" />
    <span class="validity">span>
  div>
  <p class="fallbackLabel">What month would you like to visit us?p>
  <div class="fallbackDatePicker">
    <div>
      <span>
        <label for="month">Month:label>
        <select id="month" name="month">
          <option selected>Januaryoption>
          <option>Februaryoption>
          <option>Marchoption>
          <option>Apriloption>
          <option>Mayoption>
          <option>Juneoption>
          <option>Julyoption>
          <option>Augustoption>
          <option>Septemberoption>
          <option>Octoberoption>
          <option>Novemberoption>
          <option>Decemberoption>
        select>
      span>
      <span>
        <label for="year">Year:label>
        <select id="year" name="year">select>
      span>
    div>
  div>
form>
2 để chọn tháng được mã hóa cứng với tên của các tháng, vì chúng không thay đổi (loại bỏ nội địa hóa ra khỏi mọi thứ). Danh sách các giá trị năm có sẵn được tạo động tùy thuộc vào năm hiện tại (xem các nhận xét mã bên dưới để biết giải thích chi tiết về cách các chức năng này hoạt động).

div {
  margin-bottom: 10px;
  position: relative;
}

input[type="number"] {
  width: 100px;
}

input + span {
  padding-right: 30px;
}

input:invalid + span::after {
  position: absolute;
  content: "✖";
  padding-left: 5px;
}

input:valid + span::after {
  position: absolute;
  content: "✓";
  padding-left: 5px;
}

JavaScript

Mã JavaScript xử lý việc chọn cách sử dụng phương pháp nào và thiết lập danh sách các năm để đưa vào năm không bản địa

<form>
  <div class="nativeDatePicker">
    <label for="month-visit">What month would you like to visit us?label>
    <input type="month" id="month-visit" name="month-visit" />
    <span class="validity">span>
  div>
  <p class="fallbackLabel">What month would you like to visit us?p>
  <div class="fallbackDatePicker">
    <div>
      <span>
        <label for="month">Month:label>
        <select id="month" name="month">
          <option selected>Januaryoption>
          <option>Februaryoption>
          <option>Marchoption>
          <option>Apriloption>
          <option>Mayoption>
          <option>Juneoption>
          <option>Julyoption>
          <option>Augustoption>
          <option>Septemberoption>
          <option>Octoberoption>
          <option>Novemberoption>
          <option>Decemberoption>
        select>
      span>
      <span>
        <label for="year">Year:label>
        <select id="year" name="year">select>
      span>
    div>
  div>
form>
2 sau đó.

Một phần của ví dụ có thể được quan tâm nhất là mã phát hiện tính năng. Để phát hiện xem trình duyệt có hỗ trợ

<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
6 hay không, chúng tôi sẽ tạo một phần tử
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
2 mới, hãy thử đặt
<form>
  <label for="bday-month">What month were you born in?label>
  <input id="bday-month" type="month" name="bday-month" />
form>
2 thành
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3, sau đó kiểm tra ngay kiểu của nó. Các trình duyệt không hỗ trợ loại
<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
3 sẽ trả về
<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required
      pattern="[0-9]{4}-[0-9]{2}" />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
8, vì đó là tháng rơi trở lại khi không được hỗ trợ. Nếu
<form>
  <div>
    <label for="month">
      What month would you like to visit (June to Sept.)?
    label>
    <input
      id="month"
      type="month"
      name="month"
      min="2022-06"
      max="2022-09"
      required />
    <span class="validity">span>
  div>
  <div>
    <input type="submit" value="Submit form" />
  div>
form>
6 không được hỗ trợ, chúng tôi ẩn người chọn bản địa và hiển thị giao diện người chọn Fallback Picker.

<label for="bday-month">What month were you born in?label>
<input id="bday-month" type="month" name="bday-month" />
1

Lưu ý: Hãy nhớ rằng một số năm có 53 tuần trong đó (xem tuần mỗi năm)! Bạn sẽ cần xem xét điều này khi phát triển các ứng dụng sản xuất. Remember that some years have 53 weeks in them (see Weeks per year)! You'll need to take this into consideration when developing production apps.

Thông số kỹ thuật

Sự chỉ rõ
HTML Tiêu chuẩn # Tháng-State- (Loại = Tháng)
# month-state-(type=month)

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

Tháng có phải là một phần của thuộc tính loại đầu vào trong HTML5 không?

HTML5 có các loại đầu vào mới sau: Màu sắc, ngày, DateTime, DateTime-local, tháng, tuần, thời gian, email, số, phạm vi, tìm kiếm, tel và url..

Các thuộc tính loại đầu vào trong HTML5 là gì?

Attributes.

Ngày có phải là một phần của thuộc tính loại đầu vào trong HTML5 không?

Phần tử đầu vào có thuộc tính loại có giá trị là "Ngày" biểu thị điều khiển để đặt giá trị của phần tử thành chuỗi đại diện cho một ngày..

Loại đầu vào là gì trong HTML là gì?

Các yếu tố của loại tháng tạo ra các trường đầu vào cho phép người dùng nhập một tháng và năm cho phép một tháng và năm dễ dàng nhập.Giá trị là một chuỗi có giá trị ở định dạng "Yyyy-MM", trong đó Yyyy là năm bốn chữ số và mm là số tháng.create input fields that let the user enter a month and year allowing a month and year to be easily entered. The value is a string whose value is in the format " YYYY-MM ", where YYYY is the four-digit year and MM is the month number.