js实现全选和取消全选

摘要:
HTMl“checkbox“name=”checkbox“/˃”checkbox“name=”复选框“/˃js//$()。属性/$()。data//判断是否全选。单击//判断未选中状态$中是否存在未选中的全选按钮。单击
HTMl

     <input type="checkbox" name="allCheckBox" />

     <br/>

      <input type="checkbox" name="checkbox" />

       <input type="checkbox" name="checkbox" />

      <input type="checkbox" name="checkbox" />

js
<script>
    // $().attr('data-id')
    // $().data('id')
    //判断全选 或者 全不选
$('input[name="allCheckBox"]').click(function(){
  if($(this).is(':checked')){
    $('input[name="checkbox"]').each(function(){
      //此处如果用attr,会出现第三次失效的情况
      $(this).prop("checked",true);
    });
        var num=0;
        $('.jr').each(function(){
            num += parseFloat($(this).text());
        });
        $('#J_Total').text(num);



  }else{
    $('input[name="checkbox"]').each(function(){
      $(this).prop("checked",false);
    });
        $('#J_Total').text('0.00');
  }
});

//判断当全选时 若有一个不选 全选按钮为不选状态
$('input[name="checkbox"]').click(function(){
  var checkedLength = $("input[name='checkbox']:checked").length;
  var checkLength = $("input[name='checkbox']").length;
    var cont = 0;
    $("input[name='checkbox']:checked").each(function(){
        cont +=parseFloat($(this).parents('.td-chk').siblings('.td-sum').find('.jr').text());
    });
    $('#J_Total').text(cont);

  if(checkLength == checkedLength){
      $("input[name='allCheckBox']").prop("checked",true);
      return true;
    }else{
      $("input[name='allCheckBox']").prop("checked",false);
      return true;
    }
})
</script>

免责声明:文章转载自《js实现全选和取消全选》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇彻底解决Delphi的DBGrid控件鼠标滚轮问题(转)mui学习下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

你不知道的前端js面试题

1、为什么要尽量少使用iframe? Iframes 阻塞页面加载 及时触发 window 的 onload 事件是非常重要的。onload 事件触发使浏览器的 “忙” 指示器停止,告诉用户当前网页已经加载完毕。当 onload 事件加载延迟后,它给用户的感觉就是这个网页非常慢。 window 的 onload 事件需要在所有 iframe 加载完毕后(包...

在vue或js中的异或解密和aes(des)配合使用

简单粗暴,直接贴代码: 1 //AES解密 2 decryptData(toDecrypt) { 3 var key='mochouhuaNumber1';//密钥(和后端约定的) 4 var base64 = CryptoJS.enc.Utf8.parse(key); 5 var iv=CryptoJS.enc.U...

js判断浏览器类型

js判断浏览器类型 <script type="text/javascript" ><!-- function getOs() { var OsObject = ""; if(isIE = navigator.userAgent.indexOf("MSIE")!=-1) { return "MSIE"; } if(isFirefox=na...

原生JS实现九宫格拼图

实现这个案例,需要考虑到鼠标的拖拽效果(onmousedown/onmousemove/mouseup) 拖拽分解: 按下鼠标---->移动鼠标----->松开鼠标 1.给目标元素添加onmousedown事件,拖拽的前提是在目标元素按下鼠标左键 2.当onmousedown事件发生后,此刻给document添加onmousemove事件,意味着...

JS邮箱验证-正则验证

<form action="">输入:<input type="text" name="mazey" placeholder="请输入邮箱"><input type="button" value="验证" onclick="check();"></form> <script>function ch...

js常用API方法

String对象常用的API:API指应用程序编程接口,实际上就是一些提前预设好的方法。 charAt() 方法可返回指定位置的字符。 stringObject.charAt(index) indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。 stringObject.indexOf(searchvalue, fromindex) s...