Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js checkbox checked

作者:诸神之翼   发布日期:2025-03-19   浏览:109

<!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>

解释说明:

  1. HTML 部分:

    • 创建了一个表单 (<form>),其中包含一个复选框 (<input type="checkbox">) 和一个按钮 (<button>)。
    • 复选框的 idchkBox,用于通过 JavaScript 进行操作。
    • 按钮点击时会调用 checkStatus() 函数。
  2. JavaScript 部分:

    • checkStatus() 函数用于检查复选框的状态。
    • 使用 document.getElementById("chkBox") 获取复选框元素。
    • 使用 checkBox.checked 属性判断复选框是否被选中。
    • 根据复选框的状态,更新页面中的 <p> 元素内容,显示复选框是否被选中。

上一篇:js copy数组

下一篇:nodejs opencv

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组复制

js 复制数组

js 数组拷贝

js 对象转数组

js 深拷贝数组

js 获取今天年月日

js jsonp

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站