js全选和反选

来源:互联网 发布:2017淘宝装修教程视频 编辑:程序博客网 时间:2024/05/22 14:11

转载:http://blog.csdn.net/shanliangliuxing/article/details/7410172

<!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>JS全选、反选</title>
<script type="text/javascript">
//Jquery里面引用对象的用法
function $(id){
return document.getElementById(id);
}
window.onload=function(){
var selectAll = $("selectAll"),
unSelect = $("unSelect"),
inputs=document.getElementsByName('range'),
len = inputs.length;
selectAll.onclick=function(){
for(var i=0; i<len;i++){
inputs[i].checked=true;
}
}
unSelect.onclick=function(){
for(var i=0; i<len;i++){
var o = inputs[i];
o.checked?o.checked=false:o.checked=true;
}
}
}
</script>
</head>
<body>
<form name="form1" >
  <input type="checkbox" value="1" name="range">
  1<br>
  <input type="checkbox" value="3" name="range">
  3<br>
  <input type="checkbox" value="4" name="range">
  4<br>
  <input type="checkbox" value="5" name="range">
  5<br>
  <input type="button" value="全选" id="selectAll">
  <input type="button" value="反选" id="unSelect">
</form>
</body>
</html>

0 0
原创粉丝点击