table表格中如何全选---以及复选框实现反选

table表格中如何全选,代码如下:

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>
 <table class="table table-striped table-bordered table-hover dataTables-example">
    <thead>
        <tr>
            <th><input type="checkbox" class="i-checks" id="questionCheck"/></th>
            <th>科目</th>
            <th>成绩</th>
            <th>性别</th>
        </tr>
    </thead>

    <tbody id="questionlist">
        <tr class="aa" draggable="true">
            <td><input class="i-checks" type="checkbox"></td>
            <td width="6%">语文</td>
            <td width="6%">86</td>
            <td width="6%">男</td>
        </tr>
        <tr class="aa" draggable="true">
            <td><input class="i-checks" type="checkbox"></td>
            <td width="6%">数学</td>
            <td width="6%">99</td>
            <td width="6%">男</td>
        </tr>
    </tbody>
 </table>

 <script src="./jquery-3.3.1.min.js"></script>
 <script>
   $(function(){
       $("#questionCheck").click(function () {
           var b = $('#questionlist tr.aa input[type="checkbox"]');
           if ($(this).prop('checked')) {
               b.prop('checked', true);
           } else {
               b.prop('checked', false);
           }
       });
   })
 </script>
 </body>
</html>

效果如图所示:样式难看,凑合着看吧

    

三种方法实现对多选框的反选,代码如下:

<body>
    <h1>遍历jQuery对象的内部的DOM对象</h1>
    <input type="button" value="反选" onclick="fan()" />
    <p>选择1:<input type="checkbox" /></p>
    <p>选择1:<input type="checkbox" /></p>
    <p>选择1:<input type="checkbox" /></p>
    <p>选择1:<input type="checkbox" /></p>
    <p>选择1:<input type="checkbox" /></p>
</body>

<script type="text/javascript">
    //方法一 面向过程的思路
    function fan(){
        var inp=$('input:checkbox');//获取input 取出的是jq对象
        for(var i=0;i<inp.length;i++){ //循环取出每一行的input
            //判断input是否有checked 这里inp[i]是dom对象,还要加$()转成dom对象
            if($(inp[i]).prop('checked')==true){
                $(inp[i]).prop('checked',false);  //改checked值为false
            }else {
                $(inp[i]).prop('checked',true);   //改checked值为true
            }
        }
    }

    //方法二  面向函数式的思路, 回调函数
    function fan(){
        $('input:checkbox').each(function(){
	    console.log(this);
	    //console.log($('input:checkbox'));
	    if($(this).prop('checked')==true){
	        $(this).prop('checked',false);  //改checked值为false
	    }else {
	        $(this).prop('checked',true);   //改checked值为true
	    }
        });
    }

    //方法三 更简化的方式, 直接对自己取反
    function fan(){
        $('input:checkbox').each(function(){
	    this.checked=!this.checked;
        });
    }
</script>  

 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值