使用jquery 对checkbox的操作

来源:互联网 发布:qq群监控软件 编辑:程序博客网 时间:2024/04/30 06:40
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>list page</title>

<script type="text/javascript" src="/ipsm/js/jqwidgets/scripts/jquery-1.10.2.min.js"></script>


<script type="text/javascript">
$(document).ready(function(){
//使用checkedbox管理
$("input[name=all]").click(function(){
var value=$("input[name=all]").is(":checked");
var books=$("input[name=cbx]");
value?books.attr("checked",true):books.attr("checked",false);
});


//全选
$("input[name=btnOk]").click(function(){
$("input[name=cbx]").attr("checked",true);
checkAll();
});
//全不选
$("input[name=btnNo]").click(function(){
$("input[name=cbx]").attr("checked",false);
checkAll();
});


//反选
$("input[name=btnReturn]").click(function(){
$("input[name=cbx]").each(function(){
var value=$(this).is(":checked");
if(value==true){
$(this).attr("checked",false);
}else{
$(this).attr("checked",true);
}
});
checkAll();
});
$("input[name=cbx]").click(function(){
checkAll();
})


function checkAll(){
var countOK=0;
var countNO=0;
$("input[name=cbx]").each(function(){
var value=$(this).is(":checked");
if(value==true){
countOK++;
}else{
countNO++;
}

});  
if(countOK==3){
$("input[name=all]").attr("checked",true);
}
if(countNO==3){
$("input[name=all]").attr("checked",false);
}


}
checkAll();

});  




    
</script>
</head>
<body class='default'>
<div>
<table border="1" id="newsTable">
<tr>
<th><input type="checkbox" name="all" /></th>
<th>Id</th>
<th>作者</th>
</tr>
<tr>
<td><input type="checkbox" name="cbx" /></td>
<td>1</td>
<td>阿会楠</td>
</tr>
<tr>
<td><input type="checkbox" name="cbx" /></td>
<td>2</td>
<td>阿会楠</td>
</tr>
<tr>
<td><input type="checkbox" name="cbx" /></td>
<td>3</td>
<td>阿会楠</td>
</tr>
<tr>
<td colspan="3"><input type="button" name="btnOk" id="btnOk" value="全选" /></td>
<td colspan="3"><input type="button" name="btnNo" id="btnNo" value="全不选" /></td>
<td colspan="3"><input type="button" name="btnReturn" id="btnReturn" value="反选" /></td>
</tr>
</table>
</div>
</body>
</html>