本文主要是介绍js实现全选与反选,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<title>js实现全选与反选 - 烈火网 Liehuo.Net</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
// --列头全选框被单击---
function ChkAllClick(sonName, cbAllId){
var arrSon = document.getElementsByName(sonName);
var cbAll = document.getElementById(cbAllId);
var tempState=cbAll.checked;
for(i=0;i<arrSon.length;i++) {
if(arrSon[i].checked!=tempState)
arrSon[i].click();
}
}
// --子项复选框被单击---
function ChkSonClick(sonName, cbAllId) {
var arrSon = document.getElementsByName(sonName);
var cbAll = document.getElementById(cbAllId);
for(var i=0; i<arrSon.length; i++) {
if(!arrSon[i].checked) {
cbAll.checked = false;
return;
}
}
cbAll.checked = true;
}
// --反选被单击---
function ChkOppClick(sonName){
var arrSon = document.getElementsByName(sonName);
for(i=0;i<arrSon.length;i++) {
arrSon[i].click();
}
}
</script>
</head>
<body>
<form name="form1" method="post" action="">
<table width="500" border="1">
<tr bgcolor="#FFFF66">
<td>
<INPUT name="chkAll" id="chkAll" title="全选" onClick="ChkAllClick('chkSon','chkAll')" type="checkbox" />全选</td>
<td align="center">复选框全选示例 </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><INPUT name="chkSon" id="chkSon1" type="checkbox" value='1' οnclick="ChkSonClick('chkSon','chkAll')" />1</td>
<td> 作用: </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><INPUT name="chkSon" id="chkSon2" type="checkbox" value='2' οnclick="ChkSonClick('chkSon','chkAll')" />2</td>
<td> a.单击列头复选框全选或全不选子项 </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><INPUT name="chkSon" id="chkSon3" type="checkbox" value='3' οnclick="ChkSonClick('chkSon','chkAll')" />3</td>
<td> b.只要有一个子项没有选中,则取消列头的选中状态 </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><INPUT name="chkSon" id="chkSon4" type="checkbox" value='4' οnclick="ChkSonClick('chkSon','chkAll')" />4</td>
<td> c.当所有子项目选中时,列头复选框自动置为选中状态 </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><INPUT name="chkSon" id="chkSon5" type="checkbox" value='5' οnclick="ChkSonClick('chkSon','chkAll')" />5</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><INPUT name="chkSon" id="chkSon6" type="checkbox" value='6' οnclick="ChkSonClick('chkSon','chkAll')" />6</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><INPUT name="chkSon" id="chkSon7" type="checkbox" value='7' οnclick="ChkSonClick('chkSon','chkAll')" />7</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><INPUT name="chkSon" id="chkSon8" type="checkbox" value='8' οnclick="ChkSonClick('chkSon','chkAll')" />8</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><INPUT name="chkSon" id="chkSon9" type="checkbox" value='9' οnclick="ChkSonClick('chkSon','chkAll')" />9</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><INPUT name="chkSon" id="chkSon10" type="checkbox" value='10' οnclick="ChkSonClick('chkSon','chkAll')" />10</td>
<td>d.将复选框反过来选</td>
<td> </td>
<td> </td>
</tr>
<tr bgcolor="#66FFFF">
<td><INPUT name="chkOpposite" id="chkOpposite" title="反选" onClick="ChkOppClick('chkSon')" type="checkbox" />反选</td>
<td align="center">反选示例</td>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
======++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript复选框反选与全选删除表格行等简单功能-xw素材网</title>
<style type="text/css">
table{
border:#0FF 1px solid;
border-collapse:collapse;
}
table th{
text-align:center;
}
table td{
border:1px solid #999;
text-align:center;
}
.one{
background:#9FC;
}
.two{
background:#FC6;
}
</style>
<script type="text/javascript" language="javascript">
function gehang(){
//获取tr节点
var tr=document.getElementsByTagName("tr");
//为第一行添加背景颜色
tr[0].style.background="#00FF66";
//为最后一行添加背景颜色
tr[tr.length-1].style.background="#00FF66";
}
//创建全选反选函数
function xuan(type){
//获取name值
var qcheck=document.getElementsByName("check[]");
//获取选的按钮
if(type=="qx"){
for(var i=0;i<=qcheck.length;i++){
qcheck[i].checked=true;
}
}
if(type=="fx"){
for(var i=0;i<=qcheck.length;i++){
qcheck[i].checked=false;
}
}
if(type=="qxx"){
for(var i=0;i<=qcheck.length;i++){
if(qcheck[i].checked){
qcheck[i].checked=false;
}
else{
qcheck[i].checked=true;
}
}
}
}
function del(){
var input=document.getElementsByName("check[]");
for(var i=input.length-1; i>=0;i--){
if(input[i].checked==true){
//获取td节点
var td=input[i].parentNode;
//获取tr节点
var tr=td.parentNode;
//获取table
var table=tr.parentNode;
//移除子节点
table.removeChild(tr);
}
}
}
</script>
</head>
<body οnlοad="gehang()">
<table width="400" border="0" align="center">
<tr>
<th><input οnclick="xuan('qx')" type="checkbox">全选</th>
<th>发件人</th>
<th>邮件名称</th>
<th>邮件附属信息</th>
</tr>
<tr class="one">
<td><input type="checkbox" name="check[]"></td>
<td>张三11</td>
<td>我是邮件11</td>
<td>我是附属信息11</td>
</tr>
<tr class="two">
<td><input type="checkbox" name="check[]"></td>
<td>张三12</td>
<td>我是邮件12</td>
<td>我是附属信息12</td>
</tr>
<tr class="one">
<td><input type="checkbox" name="check[]" /></td>
<td>张三13</td>
<td>我是邮件13</td>
<td>我是附属信息13</td>
</tr>
<tr class="two">
<td><input type="checkbox" name="check[]"></td>
<td>张三14</td>
<td>我是邮件14</td>
<td>我是附属信息14</td>
</tr>
<tr class="one">
<td><input type="checkbox" name="check[]"></td>
<td>张三15</td>
<td>我是邮件15</td>
<td>我是附属信息15</td>
</tr>
<tr class="two">
<td><input type="checkbox" name="check[]"></td>
<td>张三16</td>
<td>我是邮件16</td>
<td>我是附属信息16</td>
</tr>
<tr class="one">
<td><input type="checkbox" name="check[]"></td>
<td>张三17</td>
<td>我是邮件17</td>
<td>我是附属信息17</td>
</tr>
<tr class="two">
<td><input type="checkbox" name="check[]"></td>
<td>张三18</td>
<td>我是邮件18</td>
<td>我是附属信息18</td>
</tr>
<tr>
<td><input type="checkbox" name="check[]"></td>
<td>张三19</td>
<td>我是邮件19</td>
<td>我是附属信息19</td>
</tr>
<tr>
<td><input type="checkbox" name="check[]"></td>
<td>张三19</td>
<td>我是邮件19</td>
<td>我是附属信息19</td>
</tr>
<tr>
<td><input type="checkbox" name="check[]"></td>
<td>张三19</td>
<td>我是邮件19</td>
<td>我是附属信息19</td>
</tr>
<tr>
<td><input type="checkbox" name="check[]"></td>
<td>张三19</td>
<td>我是邮件19</td>
<td>我是附属信息19</td>
</tr>
<tr>
<td><input type="checkbox" name="check[]"></td>
<td>张三19</td>
<td>我是邮件19</td>
<td>我是附属信息19</td>
</tr>
<tr>
<td><input type="checkbox" name="check[]"></td>
<td>张三19</td>
<td>我是邮件19</td>
<td>我是附属信息19</td>
</tr>
<tr>
<th><input οnclick="xuan('qx')" type="checkbox">全选</th>
<td colspan="3" align="center">
<input type="button" id="qx" οnclick="xuan('qx')" value="全选">
<input type="button" id="qxx" οnclick="xuan('qxx')" value="取消全选">
<input type="button" id="fx" οnclick="xuan('fx')" value="反选">
<input type="button" id="del" οnclick="del()" value="删除所选附件">
</td>
</tr>
</table>
</body>
</html>
这篇关于js实现全选与反选的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!