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

คุณสมบัติ type ของ HTML DOM Input Checkbox

คุณสมบัติ type ของ Input Checkbox ใช้คืนค่า/ตั้งค่าประเภทของ Input Checkbox

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

  • การคืนค่าสตริง
inputCheckboxObject.type
  • การตั้งค่า type เป็นค่าสตริง
inputCheckboxObject.type = stringValue

ค่าสตริงที่เป็นไปได้

ในที่นี้ stringValue สามารถเป็นค่าต่อไปนี้ได้:

stringValueรายละเอียด
checkboxกำหนดว่าประเภท input เป็น checkbox
radioกำหนดว่าประเภท input เป็น radio
textกำหนดว่าประเภท input เป็น text

ตัวอย่าง

ตัวอย่างการใช้งานคุณสมบัติ Input Checkbox type:

<!DOCTYPE html> <html> <head> <title>แอตทริบิวต์ Type ของ Checkbox</title> </head> <body> <form id="Form"> <div> อื่นๆ: <input id="formCheckbox" type="checkbox" name="formCheckbox"> </div> </form> <button onclick="changeType()">เปลี่ยนประเภทของ input</button> <div id="displayDiv"></div> <script>    var typeOfInput = document.getElementById("formCheckbox");    var displayDiv = document.getElementById("displayDiv");    displayDiv.textContent = 'ประเภทของ Input: ' + typeOfInput.type function changeType(){       if(typeOfInput.type == 'checkbox'){          typeOfInput.type = 'text' displayDiv.textContent = 'ประเภทของ Input: ' + typeOfInput.type       }    } </script> </body> </html>

ผลลัพธ์

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

ก่อนคลิกปุ่ม 「เปลี่ยนประเภทของ input」 :

คุณสมบัติ type ของ HTML DOM Input Checkbox

หลังคลิกปุ่ม 「เปลี่ยนประเภทของ input」 :

คุณสมบัติ type ของ HTML DOM Input Checkbox