//html代码:
<input id="1" type="checkbox" class="dept" onclick="check(this)" />
<input id="2" type="checkbox" class="dept" onclick="check(this)" />
<input id="3" type="checkbox" class="dept" onclick="check(this)" />
//js:
//只能选择一个部门
function check(obj) {
$('.dept').attr('checked', false);//清空所有复选框
$(obj).attr('checked', true);//给当前对象赋值
}
<input id="1" type="checkbox" class="dept" onclick="check(this)" />
<input id="2" type="checkbox" class="dept" onclick="check(this)" />
<input id="3" type="checkbox" class="dept" onclick="check(this)" />
//js:
//只能选择一个部门
function check(obj) {
$('.dept').attr('checked', false);//清空所有复选框
$(obj).attr('checked', true);//给当前对象赋值
}

本文介绍了一种使用JavaScript实现在HTML中多个复选框仅能单选的逻辑。通过简单的代码片段,当用户尝试选择多个标记为'dept'类别的复选框时,系统会自动取消之前的选择,确保每次只有一个选项被选中。
5322

被折叠的 条评论
为什么被折叠?



