HTML checked 属性是一个布尔属性,指示默认情况下(当页面加载时)是否选中复选框。
我们可以将 checked 属性与 JavaScript 条件语句一起使用,以验证是否选中了复选框或单选按钮。如果选中复选框或单选按钮,则返回 true,否则返回 false。
语法
<input type = "checkbox/radio" checked/>
适用于
下面列出的元素允许使用 HTML checked 属性。
元素 | 描述 |
---|---|
<input> | HTML <input> 标签用于指定输入字段。 |
HTML checked 属性的示例
以下代码演示了 HTML 中 checked 属性的用法
复选框类型的 checked 属性
在以下示例中,我们在输入 (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 属性。
<!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>
支持的浏览器
浏览器 | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
是否支持 | Yes | Yes | Yes | Yes | Yes |