如何在 HTML 中指定一個元素應在頁面載入時預先選中?


使用 checked 屬性來指定 <input> 元素應在頁面載入時預先選中。此屬性可用於 −

<input type = "checkbox">
<input type = "radio">.

示例

你可以嘗試執行以下程式碼在 HTML 中實現 checked 屬性 −

 現場演示

<!DOCTYPE html>
<html>
   <body>
      <p>Which sports do you like?</p>
      <form action = "" method = "get">
         <input type = "checkbox" name = "vehicle" value = "football" checked> Football<br>
         <input type = "checkbox" name = "vehicle" value = "cricket" checked> Cricket<br>
         <input type = "checkbox" name = "vehicle" value = "hockey"> Hockey<br>
         <input type = "submit" value = "Submit">
      </form>
   </body>
</html>

更新於:2020-05-28

145 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

立即開始
廣告