DataTables 全选

动态生成的元素绑定事件

$("#example").on('click', "#checkall", function () {  //代码   }

 

获取动态生成的元素

$("#example").find("#checkall").prop("checked", flag);

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DatatablesDemo.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <link href="plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
    <%--<link href="plugins/DataTables/css/jquery.dataTables.css" rel="stylesheet" />--%>
    <link href="plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
    <link href="plugins/DataTables/css/dataTables.bootstrap.min.css" rel="stylesheet" />
</head>
<body style="padding:50px 0;">
     <!-- 表格开始 -->
    <table id="example" class="table table-striped table-bordered table-hover table-responsive">
        
    </table>
    <!-- 表格结束 -->
    <script src="plugins/DataTables/js/jquery.js"></script>
    <script src="plugins/DataTables/js/jquery.dataTables.min.js"></script>
    <script src="plugins/DataTables/js/dataTables.bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#example').DataTable({
                "ajax": "data/objects.ashx?key=getArea",
                "language": {
                    "url": "http://cdn.datatables.net/plug-ins/1.10.16/i18n/Chinese.json"
                },
                "order": [[1, "asc"]],
                "columns": [
                    {
                        data: null,
                        className: "text-center",  
                        title: "<input type='checkbox' name='checklist' id='checkall'/>"
                    },
                    { data: "id",title:"序号" },
                    { data: "MobileNumber", title: "手机号码" },
                    { data: "MobileArea", title: "所属地区" },
                    { data: "MobileType", title: "运营商" },
                    { data: "AreaCode", title: "地区码" },
                    { data: "PostCode", title: "传输码" }
                ],
                "columnDefs": [{
                    //   指定第一列,从0开始,0表示第一列,1表示第二列……
                    targets: 0,
                    render: function (data, type, row, meta) {
                        return '<input type="checkbox" name="checklist" value="' + row.id + '" />'
                    }
                }, {
                    "orderable": false, "targets": [0, -1],  //设置第一列和最后一列不可排序
                }]
            });


            //全选
            $("#example").on('click', "#checkall", function () {
                $("input[name='checklist']").prop("checked", $(this).prop("checked"));
            });

            //全选框是否选中
            $("#example").on('click', "input[name='checklist']:not('#checkall')", function () {
                var flag = true;
                $("#example").find("input[name='checklist']:not('#checkall')").each(function () {
                    if (!this.checked) {
                        flag = false;
                    }
                });
                $("#example").find("#checkall").prop("checked", flag);
            });

        });
</script>
</body>
</html>

 

转载于:https://www.cnblogs.com/linyongqin/articles/7733644.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值