jquery写个checkbox——类似邮箱全选功能

时间:2013-03-19    点击:59   
以前用原生 JS 写过 checkbox——类似邮箱全选功能,点击这里。最近在学习jquery,今天抽空用jquery 写个checkbox——类似邮箱全选功能。
复制代码 代码如下:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>checkbox</title>
</head>
<body>
<input type="checkbox" name="btn" id="btn"/><label for="btn">全选/全不选</label><br/>
<input type="checkbox" name="choose" id="checkbox1"/><label for="checkbox1">选项1</label><br/>
<input type="checkbox" name="choose" id="checkbox2"/><label for="checkbox2">选项2</label><br/>
<input type="checkbox" name="choose" id="checkbox3"/><label for="checkbox3">选项3</label><br/>
<input type="checkbox" name="choose" id="checkbox4"/><label for="checkbox4">选项4</label><br/>
<input type="checkbox" name="choose" id="checkbox5"/><label for="checkbox5">选项5</label><br/>
<input type="checkbox" name="choose" id="checkbox6"/><label for="checkbox6">选项6</label><br/>
<input type="checkbox" name="choose" id="checkbox7"/><label for="checkbox7">选项7</label><br/>
<input type="checkbox" name="choose" id="checkbox8"/><label for="checkbox8">选项8</label><br/>
<input type="checkbox" name="choose" id="checkbox9"/><label for="checkbox9">选项9</label><br/>
<input type="checkbox" name="choose" id="checkbox10"/><label for="checkbox10">选项10</label><br/>
<a href="javascript:;" id="btn2">反选</a>
</body>
</html>
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
var checkboxes = $('input[name=choose]');
var btn = $('#btn');
var btn2 = $('#btn2');
btn.click(function(){
checkboxes.attr('checked',this.checked);
});
checkboxes.click(function(){
var flag = true;
checkboxes.each(function(){
if(!this.checked) flag = false;
});
btn.attr('checked',flag);
});
btn2.click(function(){
var flag = true;
checkboxes.each(function(){
this.checked = !this.checked;
if(!this.checked) flag = false;
});
btn.attr('checked',flag);
});
});
</script>

小提示:如果使用 jquery,则需要引入 jquery 库。
PS:以上是本人通过所学的 jquery 知识,随意写的一些效果。
jQuery实现id模糊查询的小例子
JS文本框不能输入空格验证方法
getComputedStyle与currentStyle获取样式(style/class)
关于scrollLeft,scrollTop的浏览器兼容性测试
input输入框的自动匹配(原生代码)
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved