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

HTML <input> checked Attribute


แอตทริบิวต์ checked ขององค์ประกอบ <input> ระบุว่า input type checkbox ถูก checked เมื่อหน้าเว็บโหลด คุณยังสามารถใช้แอตทริบิวต์นี้กับ input type radio ได้

Syntax ดังนี้:

<input type="checkbox" checked>

ด้านบน เราตั้ง checked เนื่องจากเราต้องการให้ checkbox ถูกเลือกเมื่อหน้าเว็บโหลด

มาดูตัวอย่างการใช้ checked attribute ของ <input> element:

ตัวอย่าง

<!DOCTYPE html>
<html>
<body>
<h2>Register</h2>
<form action="" method="get">
Id: <input type="text" name="id" placeholder="Enter UserId here..." size="25"><br>
Password: <input type="password" name="pwd" placeholder="Enter password here..."><br>
DOB: <input type="date" name="dob" placeholder="Enter date of birth here..."><br>
Telephone: <input type="tel" name="tel" placeholder="Enter mobile number here..."><br>
Email: <input type="email" name="email" placeholder="Enter email here..." size="35"><br><br>
<input type="checkbox" name="vehicle" value="Bike" checked>Newsletter Subscription:<br>
<button type="submit" value="Submit">Submit</button>
</form>
</body>
</html>

ผลลัพธ์

HTML <input> checked Attribute

ในตัวอย่างข้างต้น เรามีฟอร์มพร้อมปุ่ม:

<form action="" method="get">
Id: <input type="text" name="id" placeholder="Enter UserId here..." size="25"><br>
Password: <input type="password" name="pwd" placeholder="Enter password here..."><br>
DOB: <input type="date" name="dob" placeholder="Enter date of birth here..."><br>
Telephone: <input type="tel" name="tel" placeholder="Enter mobile number here..."><br>
Email: <input type="email" name="email" placeholder="Enter email here..." size="35"><br><br>
<input type="checkbox" name="vehicle" value="Bike" checked>Newsletter Subscription:<br>
<button type="submit" value="Submit">Submit</button>
</form>

ด้วยนั้น เรายังตั้งค่า checkbox:

<input type="checkbox" name="vehicle" value="Bike" checked>Newsletter Subscription:

เห็นได้ว่าเราตั้ง checked attribute ดังนั้น input type checkbox จะยังคง checked เมื่อหน้าโหลด