带有jquery数据表的复选框单击事件不起作用

时间:2018-09-19 12:41:41

标签: javascript jquery

我已经将jQuery数据表https://datatables.net/用于我的网格。 有两个网格,每个网格中都有一个复选框。选中/取消选中复选框时,我需要生成一个事件。

我已经尝试过了,但是没有用。请让我知道我在这里做错了..........

  $(document).ready(function () {
            $('.cBSAGrid tr td').click(function (event) {
                alert('0'); 
            });
        });

 $(document).ready(function () {
            $('#BSAGrid tr').click(function (event) {
                alert('0'); 
            });
        });

我的数据表jQuery

$.ajax({
                type: "POST",
                url: "/BSA/BSAExceptionGrid",
                data: '{ policynumber: "' + txtpolicy.val() + '",clientname:"' + clientname.val() + '" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    div.css("display", "none"); 

                    $('#BSAGrid').DataTable({
                        paging: false,
                        searching: false,
                        destroy: true,
                        "bInfo" : false,
                        data: response,
                        columns: [
                            { "data": "Logged_ID" },
                            { "data": "Logged_ID" }, 
                            { "data": "CustomerName" },
                            { "data": "ClientType" }                           

                        ], 
                        aoColumnDefs: [
                        {
                            aTargets: [0],    // Column number which needs to be modified
                            mRender: function (o, v) {   // o, v contains the object and value for the column
                                return '<input type="checkbox" id="chkBSA" name="chkBSA" />';
                            }
                            //,sClass: 'tableCell'       // Optional - class to be applied to this table cell
                        }
                        ,
                         {
                             aTargets: [1],
                             visible: false
                         }
                        ],
                        select: {
                            style: 'os',
                            selector: 'td:first-child'
                        },
                        order: [[1, 'asc']]

                    });

                },
                failure: function (response) {
                    alert(response);
                },
                error: function (response) {
                    alert(response.responseText);
                }
            }); 

HTML

  <div id="BSAGridContainer" style="display:none;">  
       <table id="BSAGrid" class="cBSAGrid table table-responsive">
            <thead>
                <tr> 
                    <th></th>
                    <th >Logged_ID</th> 
                    <th>Client Name</th>
                    <th>Client Type</th>
                </tr>
            </thead> 
        </table> 
    </div>

3 个答案:

答案 0 :(得分:0)

尝试以下代码:

$(document).ready(function(){
$(document).on("change", "#chkBSA", function() {
alert('click')
});
});

答案 1 :(得分:0)

您可以在同一列中包含onChange事件侦听器,并将其添加到您的列中,返回文本onChange =“ gridCheckboxChange(this);”

答案 2 :(得分:0)

如果您正在使用bootstrap 4自定义复选框,则将一个类添加到您的复选框中

.className {left: 0;z-index: 1;width: 1.25rem;height: 1.25rem;}