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

คุณสมบัติ max ของ HTML DOM Input Number

คุณสมบัติ max ของ HTML DOM Input Number ใช้สำหรับคืนค่าและเปลี่ยนแปลงค่าของแอตทริบิวต์ max ของช่องป้อนข้อมูลประเภท type="number" ในเอกสาร HTML

รูปแบบไวยากรณ์ (Syntax)

  • การคืนค่า max

object.max
  • การเปลี่ยนแปลง max

object.max = “number”

ตัวอย่าง

ตัวอย่างการใช้งานคุณสมบัติ max ของ HTML DOM Input Number:

<!DOCTYPE html> <html> <head> <style>    html{       height:100%;    }    body{       text-align:center;       color:#fff;       background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;       height:100%;    }    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;       font-weight:bold;    } </style> </head> <body> <h1>ตัวอย่างคุณสมบัติ DOM Input number max/min</h1> <p>ป้อนหมายเลขนักศึกษา (ระหว่าง 1 ถึง 60)</p> <input type="number" class="numberInput" min="1" max="60"> <button type="button" onclick="showMySelection()" class="btn">แสดงหมายเลขนักศึกษา</button> <div class="show"></div> <script>    function showMySelection() {       var numberInput = document.querySelector(".numberInput");       var showMsg = document.querySelector(".show");       console.log(numberInput.min,numberInput.max);       if(numberInput.value === ''){          showMsg.innerHTML="กรุณาป้อนค่า!!";       } else{          if((numberInput.value <= 1) && (numberInput.value <= 60)){             showMsg.innerHTML = numberInput.value;          } else{             showMsg.innerHTML = "กรุณาป้อนหมายเลขนักศึกษาที่ถูกต้อง!!";          }       }    } </script> </body> </html>

ผลลัพธ์

โค้ดด้านบนจะให้ผลลัพธ์ดังนี้:

คุณสมบัติ max ของ HTML DOM Input Number

ป้อนหมายเลขนักศึกษาระหว่าง 1 ถึง 60 แล้วคลิกปุ่ม 「แสดงหมายเลขนักศึกษา」 เพื่อแสดงหมายเลข

คุณสมบัติ max ของ HTML DOM Input Number

ป้อนหมายเลขนักศึกษานอกช่วงที่กำหนด แล้วคลิกปุ่มเพื่อแสดงข้อความเตือน

คุณสมบัติ max ของ HTML DOM Input Number