คุณสมบัติ stepของ input type="month" คืนค่าและกำหนดช่วงขั้น (interval) ของเดือนที่ผู้ใช้สามารถเลือกได้ ค่าเริ่มต้นคือ 1
รูปแบบ (Syntax)
อ่านค่า:
object.step
กำหนดค่า:
object.step = "number"
ตัวอย่างการใช้งาน
ตั้งค่า step = 3 (เลือกได้ทีละ 3 เดือน/ไตรมาส):
<!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.1rem; }
input { display: block; width: 35%; border: 2px solid #fff; background-color: transparent; color: #fff; font-weight: bold; padding: 8px; margin: 1rem auto; }
.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; color: #db133a; font-weight: bold; }
</style>
</head>
<body>
<h1>ตัวอย่าง Input month step property</h1>
<p>สวัสดี เลือกเดือนสำหรับวันหยุดของคุณ</p>
<input type="month" class="monthInput">
<button type="button" onclick="setStep()" class="btn">Step = 3</button>
<button type="button" onclick="getStep()" class="btn">คลิกเพื่อดูค่า step</button>
<div class="show"></div>
<script>
function setStep() {
var monthInput = document.querySelector(".monthInput");
monthInput.step = "3";
}
function getStep() {
var monthInput = document.querySelector(".monthInput");
var showMsg = document.querySelector(".show");
showMsg.innerHTML = monthInput.step;
}
</script>
</body>
</html>
พฤติกรรมของ step attribute
- step="1" (ค่าเริ่มต้น): เลือกได้ทุกเดือน
- step="3": เลือกได้เดือนที่ 1, 4, 7, 10 (ไตรมาส)
- step="6": เลือกได้เดือนที่ 1, 7 (ครึ่งปี)
- step="12": เลือกได้แค่เดือนมกราคมของแต่ละปี
- ส่งผลต่อ UI picker และเมธอด stepUp/stepDown
การใช้งานจริง
- ฟอร์มรายงานไตรมาส (step=3)
- ฟอร์มรายงานครึ่งปี (step=6)
- ฟอร์มรายงานประจำปี (step=12)