Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

请问怎么实现tbody全部勾选后,thead的复选框也勾选

image.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    .bgRed{
        background-color: #b2dba1;
    }
</style>
<body>
<table>
    <thead>
    <tr>
        <th><input type="checkbox" id="checkAll"> 序号</th>
        <th>所属机构</th>
        <th>姓名</th>
        <th>手机号码</th>
    </tr>
    </thead>
    <tbody>
    <td ><input type="checkbox" name="_dataCheckBox">1</td>
    <td>山东</td>
    <td>张三</td>
    <td>15689547865</td>
    </tr>
    <tr>
        <td ><input type="checkbox" name="_dataCheckBox">2</td>
        <td>山东</td>
        <td>张三</td>
        <td>15689547865</td>
    </tr>
    </tbody>
</table>
<!--//其他行通过js中的appendto动态添加-->
</body>
<script type="text/javascript" src="jquery-2.1.1.min.js"></script>
<script>
    $("tbody tr").each(function(){
        var _this = this;
        $(this).children().slice(1).click(function(){//关键,过滤复选框所在的td的点击事件冲突
            $($(_this).children()[0]).children().each(function(){
                if(this.type=="checkbox"){
                    if(!this.checked){
                        this.checked = true;
                        $(_this).addClass("bgRed")
                    }else{
                        this.checked = false;
                        $(_this).removeClass("bgRed")
                    }
                }
            });
        });
    });
    
</script>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...