zoukankan      html  css  js  c++  java
  • checkbox全选/全不选,子复选框全选父复选框选中

    <input type="checkbox" class="optionListAll">/* 父复选框 */
    <input type="checkbox" name="optionList">
    <input type="checkbox" name="optionList">
    <input type="checkbox" name="optionList">
    <input type="checkbox" name="optionList">
    <input type="checkbox" name="optionList">
    <input type="checkbox" name="optionList">
    <input type="checkbox" name="optionList">
    var optionListAll = $('.optionListAll');
    var allList= $('[name="optionList"]');
    var listLength = allList.length; // 父复选框全选/全不选 optionListAll.on("click", function() { var self = $(this); var state = self.prop('checked'); allList.each(function() {
        var that = $(this); state ? that.prop('checked', true) : that.prop('checked', false); // 判断父复选框是否选中,选中则子复选框全部选中 }) }); //子复选框 allList.each(function() {   var that = $(this); that.on('click', function () {     var selectedListLength = $('[name="optionList"]:checked'); // 选中的长度     for(var i = 0; i < listLength; i++) {       if(!allList[i].checked) { // 子复选框有一个没选择,父复选框不选         optionListAll.prop('checked', false); } } if( selectedListLength.length === listLength) { // 子复选框都选择,复选框选中
          optionListAll.prop('checked', true); } });
  • 相关阅读:
    [Go] 实现websocket服务端
    [PHP] php使用event扩展的io复用测试
    [MySQL] 使用force index强制使用索引
    [Go] 使用net包作为tcp客户端读取http
    [Go] golang中的包管理
    [Go] 解决golang.org模块无法下载的问题
    [日常] linux设置环境变量
    [Go] golang定时器与redis结合
    [Go] golang定时器的使用
    [Linux] linux路由表
  • 原文地址:https://www.cnblogs.com/duanfuying/p/8287359.html
Copyright © 2011-2022 走看看