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

HTML DOM Input Month name Property: ชื่อช่องเลือกเดือน

คุณสมบัติ nameของ input type="month" คืนค่าและกำหนดแอตทริบิวต์ name ใช้ระบุชื่อเมื่อส่งข้อมูลฟอร์ม

รูปแบบ (Syntax)

อ่านค่า:

object.name

กำหนดค่า:

object.name = "text"

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

แสดงค่า name attribute ของ month input:

<!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 name property</h1>
    <input type="month" class="monthInput" value="2019-02" name="I'm Month input field!! ">
    <p>สวัสดี อยากดูชื่อของฉันไหม?</p>
    <p>กดปุ่มด้านล่างเลย</p>
    <button onclick="showName()" class="btn">แสดงชื่อ</button>
    <div class="show"></div>
    <script>
        function showName() {
            var monthInput = document.querySelector(".monthInput");
            var showMsg = document.querySelector(".show");
            showMsg.innerHTML = monthInput.name;
        }
    </script>
</body>
</html>

รูปแบบค่าของ month input

ค่า value จะอยู่ในรูปแบบ YYYY-MM (เช่น 2024-03) ไม่รวมวัน

การใช้งานกับฟอร์ม

เซิร์ฟเวอร์จะได้รับข้อมูลในรูปแบบ name=YYYY-MM (เช่น birthMonth=1997-10)