简体   繁体   English

jQuery datatable click事件未第二次触发

[英]Jquery datatable click event is not firing for second time

Jquery Click event is firing for first time but on second time it is not triggering. jQuery Click事件是第一次触发,但第二次未触发。

Datatable: 数据表:

var tableAttendance = $('#tableAttendance').dataTable ({
        "bDestroy"       : true,
        "bAutoWidth"     : false,
        "bServerSide"    : true,
        "bDeferRender"   : true,
        "sServerMethod"  : "POST",
        "sAjaxSource"    : pageUrl (),
        "sAjaxDataProp"  : "aaData",
        "aaSorting"      : [[2, 'desc']],
        "fnCreatedRow": function( nRow, aData, iDataIndex ) {
            $(nRow).attr({
                "data-toggle":"context",
                "data-target":"#attendance-context-menu"
            });
        },
        "aoColumns"      : [
        {
            "mData" : function (row, type, set) {
                return '<div>'+ row['Employee_Name'] +'</div>';
            }
        }]
    });

Jquery Event : jQuery事件:

    $(document).on('click contextmenu', '#tableAttendance tbody tr', function (e) {
    console.log("Event Triggered");
    var selectRow = $(this);

     if (!selectRow.hasClass('row_selected'))
     {
          selectRow.addClass('row_selected');
     }
     else
     {
          //Other than right click. Because i use multi-select option
          if( e.button !== 2 ) 
             selectRow.removeClass('row_selected');
     }
   });

Working for different row select event. 为不同的行选择事件工作。 But when i right click(context menu) same row more than once, the event is not triggring 但是当我多次右键单击(上下文菜单)同一行时,该事件不会触发

Just tested and your code works for me. 刚刚测试过,您的代码对我有用。

Could you please try to modify your click handler as follows: 您能否按如下所示尝试修改点击处理程序:

$(document).on('click contextmenu', '#dummy tbody tr', function () {
    var d = new Date();
    console.log("Event Triggered" + d.getMilliseconds());
});

If you are debugging with firebug it could well be that you overlooked the number in front of your logged text. 如果您使用Firebug进行调试,则很可能是您忽略了所记录文本前面的数字。

在此处输入图片说明

Messages with the exact same content logged multiple times result in the number being incremented. 多次记录的内容完全相同的消息会导致数字递增。

Adding milliseconds to the output will slightly modify the message and result in a new line of output. 将毫秒数添加到输出中将略微修改消息,并导致输出新行。

EDIT: 编辑:

A right click will result in the value e.button = 2 . 右键单击将得到值e.button = 2

This value is checked in your handler if( e.button !== 2 ) and does not execute selectRow.removeClass('row_selected'); 此值在处理程序中检查if( e.button !== 2 ) ,并且不执行selectRow.removeClass('row_selected'); because the result of the check is false . 因为检查的结果是false

Completely removing the check for e.button would get your right-click to select and deselect the rows. 完全删除对e.button的检查将使您右键单击以选择和取消选择行。

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

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