JS方法:box.checked==true,即当前复选框为选中状态。
jQuery方法:
第一种方法: $("#test1").prop(“checked”);
第二种方法: $("#test1").is(":checked");选中状态为true,未选中状态都是false。
练习 全选反选 案例
<input type="checkbox" onclick="selectAll(this)">全选
<div id="checkAll">
<input type="checkbox">1
<input type="checkbox">2
<input type="checkbox">3
<input type="checkbox">4
<input type="checkbox">5
<input type="checkbox">6
<input type="checkbox">7<br>
<a onclick="selectReverse()" style="text-decoration:none;cursor:pointer;">反选</a>
</div>
<script>
function selectAll(box){
var checkAll=document.getElementById("checkAll").children;
for(var i=0;i<checkAll.length;i++){
if(box.checked==true){
checkAll[i].checked=true;
}else{
checkAll[i].checked=false;
}
}
}
function selectReverse(){
var checkAll=document.getElementById("checkAll").children;
for(var i=0;i<checkAll.length;i++){
if(checkAll[i].checked==true){
checkAll[i].checked=false;
}else{
checkAll[i].checked=true;
}
}
}
</script>