简体   繁体   English

jQuery和数据表附加事件问题

[英]jquery and datatable attaching event issue

I have a problem with attaching an event on list of checkboxes on td,im using column filter plugin so the problem is the event is attached only on the first page, so i used the fnDrawCallback to attach the event on checkbox when drawing the datatable, the problem is when i pagintate twice or i write ab it attaches the event twice.so the checkboxes doesn't work. 我在使用列过滤器插件将事件附加到td,im的复选框列表上时遇到问题,所以问题是该事件仅附加在第一页上,因此在绘制数据表时我使用了fnDrawCallback将事件附加到了复选框上,问题是当我翻页两次或写ab时,它将事件附加两次。因此,复选框不起作用。 here's my javascript code that i use : 这是我使用的JavaScript代码:

$(document).ready(function () {
    $('#example').dataTable({
        "fnDrawCallback": function (oSettings) {
            $("[id*='UsertmpCheckbox']").on('change', function () {
                //alert(parseInt($(this).attr('id').replace(/[^0-9\.]/g, '')));
                var checkboxid = '#UserCheckbox' + parseInt($(this).attr('id').replace(/[^0-9\.]/g, ''));
                alert(checkboxid);
                if ($(checkboxid).prop('checked') == false) {
                    $(checkboxid).prop('checked', true);
                } else {
                    $(checkboxid).prop('checked', false);
                }
            });
        }
    }).columnFilter({ /*sPlaceHolder: "head:after",*/
        aoColumns: [{
            type: "checkbox",
            values: null /*[ 'Gecko', 'Trident', 'KHTML', 'Misc', 'Presto', 'Tasman', 'Webkit']*/
        }, {}, {}, {}, {
            type: "checkbox",
            values: ['A', 'B', 'C', 'U', 'X']
        }]
    });
});

the function that is attaching the event is in fnDrawCallback callback. 附加事件的函数在fnDrawCallback回调中。

thanks in advance 提前致谢

Answer :- 答:

$(document).ready( function () {
            $('#example').dataTable( {

                "fnDrawCallback": function( oSettings ) {
                    $("[id*='UsertmpCheckbox']").off('click');
                    $("[id*='UsertmpCheckbox']").on('click',function() {
                        //alert(parseInt($(this).attr('id').replace(/[^0-9\.]/g, '')));
                        var checkboxid = '#UserCheckbox'+parseInt($(this).attr('id').replace(/[^0-9\.]/g, ''));
                        alert(checkboxid);
                        if($(checkboxid).prop('checked')==false){
                            $(checkboxid).prop('checked', true);
                        }
                        else{
                            $(checkboxid).prop('checked', false);
                        }

                    });

                }
            } ).columnFilter({ /*sPlaceHolder: "head:after",*/ aoColumns: [
          {  type: "checkbox", values: null /*[ 'Gecko', 'Trident', 'KHTML', 'Misc', 'Presto', 'Tasman', 'Webkit']*/ },{},{},{},{ type: "checkbox", values: [ 'A', 'B', 'C', 'U', 'X']  } ]});
        } );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM