HTML - checked 屬性



HTML 的 `checked` 屬性是一個布林屬性,指示覆選框是否預設選中(頁面載入時)。

我們可以使用 JavaScript 條件語句和 `checked` 屬性來驗證複選框或單選按鈕是否被選中。如果複選框或單選按鈕被選中,則返回 `true`,否則返回 `false`。

語法

<input type = "checkbox/radio" checked/>

應用於

以下列出的元素允許使用 HTML checked 屬性。

元素 描述
<input> HTML <input> 標籤用於指定輸入欄位。

HTML checked 屬性示例

以下程式碼演示了在 HTML 中使用 checked 屬性。

複選框型別的 checked 屬性

在下面的示例中,我們在 input (type='checkbox') 元素內使用 HTML 'checked' 屬性,使其預設選中。

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML checked attribute</title>
</head>

<body>
   <!--example of the checked attribute-->
   <form> Select languages that you know: <br>
      <input type="checkbox" checked> Hindi  <br>
      <input type="checkbox"> English <br>
      <input type="checkbox"> Marathi <br>
      <input type="checkbox"> Telugu
   </form>
</body>

</html>

單選按鈕的 checked 屬性

考慮另一種情況,我們將把 checked 屬性與單選按鈕一起使用。

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML checked attribute</title>
</head>

<body>
   <!--Example of the checked attribute-->
   <form> Choose your gender: <br>
      <input 
         type="radio" 
         name='gender' 
         checked> Male <br>
      <input 
         type="radio" 
         name='gender'> Female
   </form>
</body>

</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
checked
html_attributes_reference.htm
廣告
© . All rights reserved.