Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Thuộc tính value của Input Month trong HTML DOM

Thuộc tính value của <input type="month"> trả về hoặc thiết lập **giá trị hiện tại** của input tháng. Định dạng: YYYY-MM (VD: 2024-03).

Cú pháp

Lấy giá trị:

monthInput.value

Thiết lập giá trị:

monthInput.value = "YYYY-MM"

Ví dụ: Hiển thị tháng đã chọn

<!DOCTYPE html>
<html>
<head>
    <style>
        html, body { height: 100%; margin: 0; font-family: sans-serif; }
        body {
            text-align: center; color: white;
            background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;
        }
        input[type="month"] {
            display: block; width: 35%; margin: 15px auto;
            border: 2px solid white; background: transparent;
            color: white; font-weight: bold; padding: 8px;
        }
        .btn {
            background: #0197F6; border: none; color: white;
            padding: 10px 20px; border-radius: 4px; width: 35%;
            margin: 20px auto; display: block; cursor: pointer;
        }
        .show { font-size: 1.5rem; color: #dc3545; font-weight: bold; margin-top: 15px; }
    </style>
</head>
<body>
    <h1>Demo Input Month value Property</h1>
    <p>Chọn tháng cho kỳ nghỉ</p>
    <input type="month" class="monthInput">
    <button onclick="showValue()" class="btn">Hiển thị giá trị</button>
    <div id="result" class="show"></div>
    <script>
        function showValue() {
            var input = document.querySelector(".monthInput");
            document.getElementById("result").textContent = "Value = " + input.value;
        }
    </script>
</body>
</html>

Kết quả

Chọn tháng (VD: 2024-07):

Thuộc tính value của Input Month trong HTML DOM

Nhấn "Hiển thị giá trị":

Thuộc tính value của Input Month trong HTML DOM

💡 Mẹo:
  • Giá trị rỗng "" nếu chưa chọn.
  • Set value bằng JS: input.value = "2024-12" (phải đúng định dạng YYYY-MM).
  • Kết hợp min, max để giới hạn phạm vi chọn.