Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

HTML DOM Input Month disabled Property: ปิด/เปิดช่องเลือกเดือน

คุณสมบัติ disabled ของ input type="month" คืนค่าและกำหนดสถานะการปิดใช้งานช่องเลือกเดือน

รูปแบบ (Syntax)

อ่านค่า:

object.disabled

กำหนดค่า:

object.disabled = true | false

ตัวอย่างการใช้งาน

สลับสถานะเปิด/ปิดช่องเลือกเดือน:

<!DOCTYPE html>
<html>
<head>
    <style>
        body { text-align: center; color: #fff; background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat; height: 100vh; }
        p { font-weight: 700; font-size: 1.2rem; }
        input { width: 35%; border: 2px solid #fff; background-color: transparent; color: #fff; font-weight: bold; padding: 8px; }
        .btn { background: #0197F6; border: none; height: 2rem; border-radius: 2px; width: 35%; margin: 2rem auto; display: block; color: #fff; outline: none; cursor: pointer; }
        .show { font-size: 1.5rem; font-weight: bold; }
    </style>
</head>
<body>
    <h1>ตัวอย่าง Input month disabled</h1>
    <p>สวัสดี กรอกเดือนเกิดของคุณ</p>
    <input type="month" class="monthInput">
    <button onclick="disEna()" class="btn">Disable/Enable</button>
    <div class="show"></div>
    <script>
        function disEna() {
            var monthInput = document.querySelector(".monthInput");
            var showMsg = document.querySelector(".show");
            showMsg.innerHTML = "";
            if (monthInput.disabled === true) {
                monthInput.disabled = false;
                showMsg.innerHTML = "ก่อนหน้านี้ปิดใช้งาน ตอนนี้เปิดใช้งานแล้ว";
            } else {
                monthInput.disabled = true;
                showMsg.innerHTML = "ก่อนหน้านี้เปิดใช้งาน ตอนนี้ปิดใช้งานแล้ว";
            }
        }
    </script>
</body>
</html>

ลักษณะเมื่อ disabled

  • ไม่สามารถคลิกหรือเปลี่ยนค่าได้
  • แสดงในสีจาง
  • ไม่ส่งค่าเมื่อ submit ฟอร์ม
  • CSS :disabled pseudo-class ใช้จัดสไตล์ได้