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

ใช้ Year Input Type ใน HTML (ด้วย jQuery Datepicker)


HTML ไม่มี input type สำหรับปี (year) โดยตรง มีแค่ type="month" เลือกเดือนและปี ถ้าต้องการแค่ปี ใช้ jQuery UI Datepicker กับ type="text"

ตัวอย่าง: เลือกปีเกิดนักศึกษา

<html>    <head>       <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>         <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>         <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">         <script type="text/javascript">          $(function() {             $( "#date" ).datepicker({ dateFormat: 'yy', changeYear: true, yearRange: '1900:2025' });          });       </script>    </head>    <body>       <form>          Student Name <br> <input type="text" name="sname"> <br>          Year of Birth <br> <input type="text" id="date"> <br>          <input type="submit" value="Submit">       </form>    </body> </html>

ตั้งค่า dateFormat: 'yy' แสดงแค่ปี changeYear: true ให้เลือกปีจาก dropdown yearRange จำกัดช่วงปี