当前位置:首页 > JavaScript

js 实现checkbox

2026-04-06 00:42:44JavaScript

实现 Checkbox 的基本方法

使用 HTML 和 JavaScript 创建一个简单的 Checkbox 并控制其状态。

HTML 结构

<input type="checkbox" id="myCheckbox"> 
<label for="myCheckbox">选择此项</label>

JavaScript 控制

const checkbox = document.getElementById('myCheckbox');

// 获取 Checkbox 状态
console.log(checkbox.checked); // 返回 true 或 false

// 设置 Checkbox 状态
checkbox.checked = true; // 勾选
checkbox.checked = false; // 取消勾选

// 监听 Checkbox 变化事件
checkbox.addEventListener('change', function() {
    if (this.checked) {
        console.log('Checkbox 被选中');
    } else {
        console.log('Checkbox 被取消选中');
    }
});

动态创建 Checkbox

通过 JavaScript 动态生成 Checkbox 并添加到页面中。

// 创建 Checkbox 元素
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = 'dynamicCheckbox';

// 创建 Label 元素
const label = document.createElement('label');
label.htmlFor = 'dynamicCheckbox';
label.textContent = '动态创建的 Checkbox';

// 添加到 DOM
document.body.appendChild(checkbox);
document.body.appendChild(label);

全选/全不选功能

实现一组 Checkbox 的全选或全不选功能。

HTML 结构

<input type="checkbox" id="selectAll"> 全选/全不选
<div id="checkboxGroup">
    <input type="checkbox" class="item"> 选项1
    <input type="checkbox" class="item"> 选项2
    <input type="checkbox" class="item"> 选项3
</div>

JavaScript 控制

const selectAll = document.getElementById('selectAll');
const checkboxes = document.querySelectorAll('.item');

// 全选/全不选功能
selectAll.addEventListener('change', function() {
    checkboxes.forEach(checkbox => {
        checkbox.checked = this.checked;
    });
});

// 单个 Checkbox 变化时更新全选状态
checkboxes.forEach(checkbox => {
    checkbox.addEventListener('change', function() {
        selectAll.checked = [...checkboxes].every(c => c.checked);
    });
});

样式自定义

通过 CSS 自定义 Checkbox 的外观。

HTML 结构

<label class="custom-checkbox">
    <input type="checkbox">
    <span class="checkmark"></span>
    自定义样式 Checkbox
</label>

CSS 样式

.custom-checkbox {
    display: block;
    position: relative;
    padding-left: 35px;
    cursor: pointer;
    user-select: none;
}

.custom-checkbox input {
    position: absolute;
    opacity: 0;
    cursor: pointer;
    height: 0;
    width: 0;
}

.checkmark {
    position: absolute;
    top: 0;
    left: 0;
    height: 25px;
    width: 25px;
    background-color: #eee;
}

.custom-checkbox:hover input ~ .checkmark {
    background-color: #ccc;
}

.custom-checkbox input:checked ~ .checkmark {
    background-color: #2196F3;
}

.checkmark:after {
    content: "";
    position: absolute;
    display: none;
}

.custom-checkbox input:checked ~ .checkmark:after {
    display: block;
}

.custom-checkbox .checkmark:after {
    left: 9px;
    top: 5px;
    width: 5px;
    height: 10px;
    border: solid white;
    border-width: 0 3px 3px 0;
    transform: rotate(45deg);
}

表单提交处理

在表单提交时获取所有被选中的 Checkbox 值。

HTML 结构

<form id="myForm">
    <input type="checkbox" name="interests" value="sports"> 体育
    <input type="checkbox" name="interests" value="music"> 音乐
    <input type="checkbox" name="interests" value="reading"> 阅读
    <button type="submit">提交</button>
</form>

JavaScript 处理

js 实现checkbox

document.getElementById('myForm').addEventListener('submit', function(e) {
    e.preventDefault();

    const selected = [];
    document.querySelectorAll('input[name="interests"]:checked').forEach(checkbox => {
        selected.push(checkbox.value);
    });

    console.log('选中的兴趣:', selected);
    // 这里可以添加表单提交逻辑
});

标签: jscheckbox
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js实现全选

js实现全选

实现全选功能的方法 在JavaScript中实现全选功能通常涉及监听全选复选框的点击事件,并根据其状态控制其他复选框的选中状态。以下是几种常见的实现方式: 基础DOM操作实现 通过获取所有目标复选框…

js实现pdf在线预览

js实现pdf在线预览

使用PDF.js实现PDF在线预览 PDF.js是由Mozilla开发的一个开源JavaScript库,可以在网页中直接渲染PDF文件。以下是实现PDF在线预览的步骤: 引入PDF.js库 在HT…

js实现论坛

js实现论坛

实现论坛的基本功能 使用JavaScript实现一个论坛需要结合前端和后端技术。前端可以使用React、Vue或Angular等框架,后端可以选择Node.js配合Express或Koa框架。数据库可…

js实现图表

js实现图表

在JavaScript中实现图表通常使用流行的图表库,以下是几种常见的方法和工具: 使用Chart.js Chart.js是一个简单灵活的库,适合快速生成响应式图表。安装方式包括CDN引入或npm安…

js实现tab选项卡切换

js实现tab选项卡切换

实现Tab选项卡切换的JavaScript方法 使用纯JavaScript实现Tab切换功能,可以通过监听点击事件动态切换内容显示状态。以下是两种常见实现方式: 基础DOM操作实现 /…