jquery 书写全选反选功能

来源:互联网 发布:不为谁而作的歌 知乎 编辑:程序博客网 时间:2024/06/08 06:15
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>全选反选插件</title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
    <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
    <div class="row6">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th style="width:250px;">
                        <button type="button" class="btn btn-success" id="check_all">全选</button>
                        <button type="button" class="btn btn-danger" id="check_others">反选</button>
                    </th>
                    <th>ID</th>
                    <th>name</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><input type="checkbox" class="son_checkbox" ></td>
                    <td>1</td>
                    <td>张三</td>
                </tr>
                <tr>
                    <td><input type="checkbox" class="son_checkbox" ></td>
                    <td>2</td>
                    <td>李四</td>
                </tr>
                <tr>
                    <td><input type="checkbox" class="son_checkbox" ></td>
                    <td>3</td>
                    <td>王五</td>
                </tr>
                <tr>
                    <td><input type="checkbox" class="son_checkbox" ></td>
                    <td>4</td>
                    <td>赵六</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<script>
    $(function(){
        //点击全选事件
        $("#check_all").click(function(){
            var text=$(this).text();
            var status;
            if(text=="全选")
            {
                status=true;
                text="取消全选";
            }
            else if(text=="取消全选")
            {
                status=false;
                text="全选";
            }else
            {
                return false;
            }
            $(".son_checkbox").prop("checked",status);//改变状态
            $(this).text(text);//设置文字
        });
        //点击反选事件
        $("#check_others").click(function(){
            //遍历所有选择框 然后反转状态
            $(".son_checkbox").each(function(){
                var now_status=$(this).prop("checked");//获取当前选择框是否选中
                now_status=!now_status;//反转状态
                $(this).prop("checked",now_status);//重新绑定状态
            });
        });
    });
</script>
</body>
</html>
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 眼睛进了石灰粉怎么办 高铁网上没票了怎么办 限行尾号是字母怎么办 手指被刺扎肿了怎么办 手上进了小刺怎么办 招投标标书丢了怎么办 冒险岛2装备红了怎么办 宝宝屁股腌红了怎么办 宝宝肛门红痒怎么办啊 宝宝屁屁溃烂了怎么办 脸过敏起红疙瘩怎么办 一岁宝宝屁股红怎么办 屁眼肉凸出来了怎么办 陶笛声音变闷了怎么办 吃三七粉上火了怎么办 红枣核吞下去了怎么办 话梅核吞下去了怎么办 芒果和海鲜吃了怎么办 小孩咳嗽喉咙有痰怎么办 4岁宝宝喉咙有痰怎么办 20天新生儿有痰怎么办 孩子嗓子老是有痰怎么办 买的哈密瓜不甜怎么办 吉他琴颈变形了怎么办 hcg值长得慢怎么办 蚊子老在耳边叫怎么办 刚买来的鲜海参怎么办 天冷手指关节疼怎么办 未满一年驾龄上高速违章怎么办 榴莲太生剥开了怎么办 榴莲开了没熟怎么办 榴莲打开了没熟怎么办 榴莲开口了没熟怎么办 榴莲没熟打开了怎么办 不熟的榴莲没熟怎么办 打开的榴莲没熟怎么办 开了的榴莲没熟怎么办 榴莲剥出来没熟怎么办 榴莲剥开了没熟怎么办 榴莲买回来没熟怎么办 整个的榴莲熟了怎么办