คุณสมบัติ disabled ของ HTML DOM Select ใช้สำหรับคืนค่าและเปลี่ยนแปลงสถานะการปิดใช้งานของรายการแบบเลื่อนลงในเอกสาร HTML
รูปแบบไวยากรณ์ (Syntax)
การคืนค่า disabled
object.disabled
การเปลี่ยนแปลง disabled
object.disabled = true | false
ตัวอย่าง
ตัวอย่างการใช้งานคุณสมบัติ disabled ของ HTML DOM Select:
<!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.2rem; } .drop-down{ width:35%; border:2px solid #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>ตัวอย่าง DOM Select disabled</h1> <p>สวัสดีครับ โปรดเลือกวิชาที่คุณชอบ:</p> <select class='drop-down'> <option>ฟิสิกส์</option> <option>คณิตศาสตร์</option> <option>เคมี</option> <option>ภาษาอังกฤษ</option> </select> <button onclick="disEna()" class="btn">ปิด/เปิดใช้งาน</button> <div class="show"></div> <script> function disEna() { var dropDown = document.querySelector(".drop-down"); var showMsg = document.querySelector(".show"); showMsg.innerHTML =""; if (dropDown.disabled === true){ dropDown.disabled = false; showMsg.innerHTML ="ก่อนหน้านี้ถูกปิดใช้งาน แต่ตอนนี้เปิดใช้งานแล้ว"; }else{ dropDown.disabled = true; showMsg.innerHTML ="ก่อนหน้านี้เปิดใช้งานอยู่ แต่ตอนนี้ถูกปิดใช้งานแล้ว"; } } </script> </body> </html>
ผลลัพธ์
โค้ดด้านบนจะให้ผลลัพธ์ดังนี้:

คลิกปุ่ม 「ปิด/เปิดใช้งาน」 เพื่อปิดหรือเปิดใช้งานรายการแบบเลื่อนลง คลิกครั้งแรกจะปิดใช้งาน:

คลิกอีกครั้งเพื่อเปิดใช้งาน:
