六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 46|回复: 0

checkbox的多选与反选 jQuery 实现

[复制链接]

升级  42%

5

主题

5

主题

5

主题

童生

Rank: 1

积分
21
 楼主| 发表于 2013-1-26 14:36:01 | 显示全部楼层 |阅读模式
<?php
if($_POST){
print_r($_POST['id']);
}
?>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript">
$(function() {
   /**  
    *   自定义函数id
*function id(id){
*return $("#"+id);
*}
*/
var all = id("all");
var not = id("not");
var td = $(".checkbox_id");
var ischeck;
all.click(function () {
not.attr("checked",false)
if(all.attr("checked")==true){
ischeck = true;
}else{
ischeck = false;
}
$.each(td,function(){
$(this).attr("checked",ischeck);
});
});
not.click(function() {
all.attr("checked",false)
$.each(td,function(){
if($(this).attr("checked") == true){
$(this).attr("checked",false);
}else{
$(this).attr("checked",true);
}
});
});
});
</script>
<style type="text/css">
td {
text-align:center;
}
.left{
text-align:left;
}
</style>
<form method="post" action="">
<table style="margin-left:200px;">
<tr>
<th style="width:60px;" class="left"></th>
    <th style="width:30px;">ID</th>
<th style="width:90px;">checkbox</th>
</tr>
<tr>
<td class="left"><input class="checkbox_id" type="checkbox" name="id[]" value="1" /></td>
<td>b</td>
<td>b</td>
</tr>
<tr>
<td class="left"><input class="checkbox_id" type="checkbox" name="id[]" value="2" /></td>
<td>c</td>
<td>c</td>
</tr>
<tr>
<td class="left"><input class="checkbox_id" type="checkbox" name="id[]" value="3" /></td>
<td>d</td>
<td>d</td>
</tr>
<tr >
<td class="left">
<input type="checkbox" id="all">全选</input><br>
<input type="checkbox" id="not">反选</input>
</td>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表