HTMLchecked 屬性
<input>元素的 checked 屬性指定在載入網頁時,輸入型別複選框已選中。還可以將此屬性用於輸入型別單選按鈕。
以下是語法 −
<input type=”checkbox” checked>
以上,我們已將其設定為 checked,因為我們希望在載入網頁時選中複選框。
現在讓我們看一個示例來實現 <input> 元素的 checked 屬性 −
示例
<!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>
輸出
在以上示例中,我們有一個帶有按鈕的表單 −
<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>
除此之外,我們還設定了一個複選框 −
<input type="checkbox" name="vehicle" value="Bike" checked>Newsletter Subscription:
如上所見,我們設定了 checked 屬性,以便在載入頁面時,輸入型別複選框保持選中狀態。
廣告