代码分析
html代码
代码如下 | 复制代码 |
<form name="myform" method="post" id="myform" action="" > <input name='id' type='checkbox' onclick='unselectall()' id='id' value='1'> 设计家园 <input name='id' type='checkbox' onclick='unselectall()' id='id' value='2'> 网页教程 <input name='id' type='checkbox' onclick='unselectall()' id='id' value='3'> 酷站欣赏 <input name='id' type='checkbox' onclick='unselectall()' id='id' value='4'> 网页素材 <input name='chkAll' type='checkbox' id='chkAll' onclick='CheckAll(this.form)' value='checkbox'> 全选 </form> |
js代码
代码如下 | 复制代码 |
<script language=javascript> function unselectall(){ if(document.myform.chkAll.checked){ document.myform.chkAll.checked = document.myform.chkAll.checked&0; } } function CheckAll(form){ for (var i=0;i<form.elements.length;i++){ var e = form.elements[i]; if (e.Name != 'chkAll'&&e.disabled==false) e.checked = form.chkAll.checked; } } </script> |
实
代码如下 | 复制代码 |
<!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=gb2312" /> <title>js实现checkbox全选,反选,全不选</title> <script type="text/javascript"> //复选框全选 function checkAll(formvalue) { var roomids = document.getElementsByName(formvalue); for (var j = 0; j < roomids.length; j++) { if (roomids.item(j).checked == false) { roomids.item(j).checked = true; } } } //复选框全不选 //复选框选择转换 </script> |
jquery代码
代码如下 | 复制代码 |
<!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 runat="server"> <title>jQuery实现CheckBox全选、全不选</title> <script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function() { $("#checkAll").click(function() { $('input[name="subBox"]').attr("checked",this.checked); }); var $subBox = $("input[name='subBox']"); $subBox.click(function(){ $("#checkAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false); }); }); </script> </head> |