<input type="checkbox" id="option1" name="option1" value="yes">
<label for="option1">选项1</label>
| 属性 | 说明 | 示例 |
|---|---|---|
type="checkbox" |
定义输入类型为复选框 | 必需 |
id |
唯一标识符,与label关联 | id="agree" |
name |
表单提交时的字段名 | name="interests[]" |
value |
选中时提交的值 | value="sports" |
checked |
默认选中状态 | checked 或 checked="checked" |
disabled |
禁用复选框 | disabled |
required |
HTML5新增,必须选中 | required |
<form>
<input type="checkbox" id="subscribe" name="subscribe">
<label for="subscribe">订阅新闻邮件</label>
</form>
<form>
<h3>选择兴趣:</h3>
<input type="checkbox" id="sports" name="interests[]" value="sports">
<label for="sports">体育</label><br>
<input type="checkbox" id="music" name="interests[]" value="music">
<label for="music">音乐</label><br>
<input type="checkbox" id="reading" name="interests[]" value="reading">
<label for="reading">阅读</label>
</form>
<!-- 方式1:使用for属性关联 -->
<input type="checkbox" id="optionA">
<label for="optionA">选项A</label>
<!-- 方式2:包裹input元素 -->
<label>
<input type="checkbox" name="optionB">
选项B
</label>
<form id="myForm">
<fieldset>
<legend>选择技能</legend>
<input type="checkbox" id="html" name="skills" value="html">
<label for="html">HTML</label>
<input type="checkbox" id="css" name="skills" value="css">
<label for="css">CSS</label>
<input type="checkbox" id="js" name="skills" value="js">
<label for="js">JavaScript</label>
</fieldset>
</form>
<input type="checkbox" id="indeterminate" onclick="toggleState()">
<label for="indeterminate">三态复选框</label>
<script>
function toggleState() {
const checkbox = document.getElementById('indeterminate');
if (!checkbox.hasAttribute('data-state')) {
checkbox.indeterminate = true;
checkbox.setAttribute('data-state', 'indeterminate');
} else if (checkbox.getAttribute('data-state') === 'indeterminate') {
checkbox.indeterminate = false;
checkbox.checked = true;
checkbox.setAttribute('data-state', 'checked');
} else {
checkbox.checked = false;
checkbox.removeAttribute('data-state');
}
}
</script>
// 单个复选框
const checkbox = document.getElementById('myCheckbox');
console.log('选中状态:', checkbox.checked);
// 复选框组
const checkboxes = document.querySelectorAll('input[name="skills"]:checked');
const selectedValues = Array.from(checkboxes).map(cb => cb.value);
console.log('选中的技能:', selectedValues);
// change事件
document.getElementById('subscribe').addEventListener('change', function(e) {
console.log('订阅状态改变:', e.target.checked);
});
// click事件
document.querySelectorAll('input[type="checkbox"]').forEach(cb => {
cb.addEventListener('click', function(e) {
console.log('点击了复选框:', this.name, this.checked);
});
});
<input type="checkbox" id="selectAll">
<label for="selectAll">全选/全不选</label>
<div id="itemList">
<input type="checkbox" class="item" value="1"> 项目1<br>
<input type="checkbox" class="item" value="2"> 项目2<br>
<input type="checkbox" class="item" value="3"> 项目3<br>
</div>
<script>
document.getElementById('selectAll').addEventListener('change', function(e) {
const items = document.querySelectorAll('.item');
items.forEach(item => {
item.checked = e.target.checked;
});
});
// 当单个项目状态改变时,更新全选复选框状态
document.querySelectorAll('.item').forEach(item => {
item.addEventListener('change', function() {
const items = document.querySelectorAll('.item');
const allChecked = Array.from(items).every(item => item.checked);
const selectAll = document.getElementById('selectAll');
selectAll.checked = allChecked;
selectAll.indeterminate = !allChecked && Array.from(items).some(item => item.checked);
});
});
</script>
/* 隐藏原生复选框 */
.custom-checkbox input[type="checkbox"] {
display: none;
}
/* 自定义复选框外观 */
.custom-checkbox .checkmark {
display: inline-block;
width: 20px;
height: 20px;
background-color: #eee;
border: 2px solid #ddd;
border-radius: 4px;
position: relative;
cursor: pointer;
vertical-align: middle;
margin-right: 8px;
}
/* 选中状态 */
.custom-checkbox input[type="checkbox"]:checked + .checkmark {
background-color: #2196F3;
border-color: #2196F3;
}
/* 勾选标记 */
.custom-checkbox input[type="checkbox"]:checked + .checkmark:after {
content: "";
position: absolute;
left: 6px;
top: 2px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px 0;
transform: rotate(45deg);
}
<div class="custom-checkbox">
<input type="checkbox" id="custom1">
<label for="custom1" class="checkmark"></label>
<label for="custom1" style="vertical-align: middle;">自定义样式复选框</label>
</div>
/* 现代复选框样式 */
.checkbox-container {
display: flex;
align-items: center;
margin-bottom: 10px;
cursor: pointer;
}
.checkbox-container input[type="checkbox"] {
opacity: 0;
position: absolute;
}
.checkbox-container .checkmark {
width: 22px;
height: 22px;
border: 2px solid #666;
border-radius: 6px;
margin-right: 12px;
position: relative;
transition: all 0.3s;
}
.checkbox-container input[type="checkbox"]:checked + .checkmark {
background-color: #4CAF50;
border-color: #4CAF50;
}
.checkbox-container input[type="checkbox"]:checked + .checkmark:after {
content: "✓";
position: absolute;
color: white;
font-size: 16px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.checkbox-container input[type="checkbox"]:disabled + .checkmark {
background-color: #e0e0e0;
border-color: #b0b0b0;
cursor: not-allowed;
}
<form id="myForm">
<input type="checkbox" name="newsletter" value="subscribe"> 订阅新闻<br>
<input type="checkbox" name="interests[]" value="tech"> 科技<br>
<input type="checkbox" name="interests[]" value="sports"> 体育<br>
<button type="button" onclick="getFormData()">获取表单数据</button>
</form>
<script>
function getFormData() {
const form = document.getElementById('myForm');
const formData = new FormData(form);
// 获取单个值
const newsletter = formData.get('newsletter'); // "subscribe" 或 null
// 获取数组值
const interests = formData.getAll('interests[]');
console.log({
newsletter: newsletter,
interests: interests
});
}
</script>
function collectFormData() {
const form = document.getElementById('myForm');
const checkboxes = form.querySelectorAll('input[type="checkbox"]:checked');
const data = {};
checkboxes.forEach(cb => {
if (cb.name.endsWith('[]')) {
const key = cb.name.slice(0, -2);
if (!data[key]) data[key] = [];
data[key].push(cb.value);
} else {
data[cb.name] = cb.value;
}
});
return JSON.stringify(data);
}
name="feature_enabled"name="interests[]" 或 name="interests"(允许多值)/* 响应式复选框 */
@media (max-width: 768px) {
.checkbox-container {
margin-bottom: 15px;
}
.checkbox-container .checkmark {
width: 28px;
height: 28px;
}
.checkbox-container input[type="checkbox"]:checked + .checkmark:after {
font-size: 20px;
}
}
默认情况下,未选中的复选框不会随表单提交。如果需要,可以添加隐藏字段:
<input type="hidden" name="newsletter" value="no">
<input type="checkbox" name="newsletter" value="yes"> 订阅新闻
HTML5 checkbox在现代浏览器中完全支持。需要注意:
indeterminate属性需要JavaScript设置通过以上内容,你应该对HTML5 checkbox有了全面的理解。记住,良好的用户体验来自清晰的标签、合理的分组和适当的视觉反馈。