<!DOCTYPE html>
<html>
<head>
<title>JS Checkbox Checked Example</title>
</head>
<body>
<h2>Checkbox Checked Example</h2>
<form id="myForm">
<input type="checkbox" id="chkBox" name="chkBox"> Check me<br>
<button type="button" onclick="checkStatus()">Check Status</button>
</form>
<p id="status"></p>
<script>
function checkStatus() {
// 获取 checkbox 元素
var checkBox = document.getElementById("chkBox");
// 检查 checkbox 是否被选中
if (checkBox.checked == true){
document.getElementById("status").innerHTML = "Checkbox is checked.";
} else {
document.getElementById("status").innerHTML = "Checkbox is not checked.";
}
}
</script>
</body>
</html>
HTML 部分:
<form>
),其中包含一个复选框 (<input type="checkbox">
) 和一个按钮 (<button>
)。id
是 chkBox
,用于通过 JavaScript 进行操作。checkStatus()
函数。JavaScript 部分:
checkStatus()
函数用于检查复选框的状态。document.getElementById("chkBox")
获取复选框元素。checkBox.checked
属性判断复选框是否被选中。<p>
元素内容,显示复选框是否被选中。上一篇:js copy数组
下一篇:nodejs opencv
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站