function checkAll() {//全选全不选 let xieyi=$("#dataTypeAll"); if(!xieyi.is(':checked')){ $("[name='dataType']").prop("checked",false) }else { $("[name='dataType']").prop("checked",true) } } function getSelectType() {//获取选中得值 var str=""; $("[name='dataType']").each(function(){ if ($(this).prop('checked') == true) { console.log($(this).val()); //打印当前选中的复选框的值 str=str+$(this).val()+","; } }); alert(str); } $("[name='dataType']").click(function () {//监听复选框 alert("点击"); var flag=true; $("[name='dataType']").each(function(){ console.log($(this).prop('checked')); if ($(this).prop('checked') == false) { flag=false; return; } }); console.log("========"); $("#dataTypeAll").prop("checked",flag); });
HTML
<div style="float: left;margin-right: 10px;"> <label class="checkbox-inline"> <input type="checkbox" name="dataType" value="rh"> 燃弧 </label> <label class="checkbox-inline"> <input type="checkbox" name="dataType" value="dl"> 电流 </label> <label class="checkbox-inline"> <input type="checkbox" name="dataType" value="wd"> 温度 </label> <label class="checkbox-inline"> <input type="checkbox" name="dataType" value="wd"> 磨耗 </label> <label class="checkbox-inline"> <input type="checkbox" name="dataType" value="wd"> 硬点 </label> <label class="checkbox-inline"> <input type="checkbox" οnchange="checkAll()" id="dataTypeAll" value="option3"> 全选/取消 </label> </div> <div><h1><span οnclick="getSelectType()" class="label label-success" >查看数据</span></h1></div>