使用$(元素对象).prop('checked',true) 使单选框选中
使用$(元素对象).prop('checked',false) 使单选框取消选中
使用自定义属性 data-checked 来获取和更新当前复选框的状态
html页面代码
<div class="batch-order">
<div class="order-body">
<h5 class="order-header">批量审批生产日志工单信息</h5>
<form name="orderMgn" action="">
<div class="order-wrapper batch-operate">
<div class="flex-content">
<div class="label">批量审批:</div>
<div class="operate">
<label> <input id="passAll" type="radio" name="next" value="0" data-checked="0">全部通过</label>
<label> <input id="denyAll" type="radio" name="next" value="1" data-checked="0">全部不通过</label>
</div>
</div>
<div class="flex-content">
<div class="label">批量日志评价:</div>
<div class="operate">
<input id="remarkAll" type="text" name="remark" value="">
</div>
</div>
</div>
</form>
<div id="order-content">
<form name="order" data-order="62382f86-b172-11ec-9a20-eeeeeeeeeeee" action="">
<input type="hidden" name="flowInstanceId" value="62382f86-b172-11ec-9a20-eeeeeeeeeeee">
<input type="hidden" name="taskId" value="627ed580-b172-11ec-9a20-eeeeeeeeeeee">
<div class="order-wrapper">
<div class="flex-content order">
<div class="orderInfo">
<img class="mgn-icon" src="/ibop/business/ufc/productionlog/image/Management_2.png" alt="">
<span class="tit">测试3</span>
<a class="detail-link" name="detail-link">查看详情</a>
</div>
<div class="orderOption">
<label> <input type="radio" name="next" value="0" checked="">通过</label>
<label> <input type="radio" name="next" value="1">不通过</label>
</div>
</div>
<div class="flex-content">
<div class="label">批量日志评价:</div>
<div class="operate">
<input type="text" name="remark" value="">
</div>
</div>
</div>
</form>
<form name="order" data-order="60968d6a-b172-11ec-9a20-eeeeeeeeeeee" action="">
<input type="hidden" name="flowInstanceId" value="60968d6a-b172-11ec-9a20-eeeeeeeeeeee">
<input type="hidden" name="taskId" value="60f157a4-b172-11ec-9a20-eeeeeeeeeeee">
<div class="order-wrapper">
<div class="flex-content order">
<div class="orderInfo">
<img class="mgn-icon" src="/ibop/business/ufc/productionlog/image/Management_2.png" alt="">
<span class="tit">测试3</span>
<a class="detail-link" name="detail-link">查看详情</a>
</div>
<div class="orderOption">
<label> <input type="radio" name="next" value="0" checked="">通过</label>
<label> <input type="radio" name="next" value="1">不通过</label>
</div>
</div>
<div class="flex-content">
<div class="label">批量日志评价:</div>
<div class="operate">
<input type="text" name="remark" value="">
</div>
</div>
</div>
</form>
<form name="order" data-order="5eef941d-b172-11ec-9a20-eeeeeeeeeeee" action="">
<input type="hidden" name="flowInstanceId" value="5eef941d-b172-11ec-9a20-eeeeeeeeeeee">
<input type="hidden" name="taskId" value="5f906807-b172-11ec-9a20-eeeeeeeeeeee">
<div class="order-wrapper">
<div class="flex-content order">
<div class="orderInfo">
<img class="mgn-icon" src="/ibop/business/ufc/productionlog/image/Management_2.png" alt="">
<span class="tit">测试3</span>
<a class="detail-link" name="detail-link">查看详情</a>
</div>
<div class="orderOption">
<label> <input type="radio" name="next" value="0" checked="">通过</label>
<label> <input type="radio" name="next" value="1">不通过</label>
</div>
</div>
<div class="flex-content">
<div class="label">批量日志评价:</div>
<div class="operate">
<input type="text" name="remark" value="">
</div>
</div>
</div>
</form></div>
</div>
<div class="commit-btn">
<div>
<button id="commit">提交</button>
<button id="cancel-btn">取消</button>
</div>
</div>
</div>
全选按钮点击事件
/**
* "全部通过" 全选的单选按钮事件
*/
$('#passAll').click(function () {
let passes = $('input[name="next"][value = "0"]'); //获取所有的 "通过" 单选按钮
let denies = $('input[name="next"][value = "1]'); //获取所有的 "不通过" 单选按钮
$.each(denies, function (index, item) { //遍历所有的 "不通过"单选框
$(item).prop("checked", false); //取消选中所有"不通过"单选按钮
})
$.each(passes, function (index, item) { //遍历所有的 "通过"单选框
$(item).prop("checked", true); // 选中所有"通过"单选按钮
})
let checked = $(this).data('checked') //获取 全选按钮的 "data-checked" 属性
setChecked(checked, this, $('#denyAll')) //更改data-checked属性,并判断在已选中的状态下取消选中
})
/**
* 全部不通过
*/
$('#denyAll').click(function () {
let passes = $('input[name="next"][value = "0"]');
let denies = $('input[name="next"][value = "1"]');
$.each(denies, function (index, item) {
$(item).prop("checked", true);
})
$.each(passes, function (index, item) {
$(item).prop("checked", false);
})
let checked = $(this).data('checked')
setChecked(checked, this, $('#passAll'))
})
function setChecked(checked, target, oppo) {
switch (checked) {
//如果当前的选中状态 data-checked = 0 (未选中),则设置data-checked = 1 选中
case 0:
$(target).data('checked', 1)
$(oppo).data('checked', 0)
break
//如果当前的选中状态 data-checked = 1 (选中的状态),则取消选中状态
case 1:
$(target).data('checked', 0)
$(target).prop('checked', false)
break
}
}