2013-07-11 53 views
1

我想要檢測用戶是否右鍵單擊表中的一行(由數據表啓用)。如何檢測數據表插件中的右鍵單擊

現在,如果我使用非AJAX源下面的代碼工作正常:

oTable.$('tr').mousedown(function(e) { 
    if (e.which === 3) { //Right Mousebutton was clicked 
     window.sData = oTable.fnGetData(this); 
     jQuery(this).contextMenu({ x: e.pageX + 10, y: e.pageY + 10}); 
    } 
}); 

但是,這是行不通的,如果我使用AJAX源,所以我環顧四周,試圖:

jQuery('#myTable tbody').on('click', 'tr', function (e) { 
    alert("a click!"); 
    if (e.which === 3) { //Right Mousebutton was clicked 
     alert("actually it was a right click!"); 
    } 
}); 

此代碼確實檢測到定期點擊,但是如果無法識別右鍵點擊。

我在做什麼錯?

+0

http://stackoverflow.com/questions/706655/bind-event-to-right-mouse-click –

+0

@Jakob:謝謝以供評論,但是一旦刷新表格,此方法就會停止工作。 – mattosmat

回答

3

Alexey的代碼在你第一次加載表的時候工作,但是當你對它執行一些ajax操作時它會停止工作。因此,必須使用.on(...)方法。

的代碼我目前使用這樣的容貌:

jQuery('#myTable tbody').on('mousedown', 'tr', function (e) { 
    alert("mouse event detected!"); 
    if(e.button == 2) { 
     alert('Right mouse button!'); 
     return false; 
    } 
    return true; 
}); 
3

像這樣的東西?

jQuery('#myTable tbody').mousedown(function(e){ 
    if(e.button == 2) { 
     alert('Right mouse button!'); 
     return false; 
    } 
    return true; 
    }); 
+0

謝謝你的幫助。它幫助我弄清楚了我必須改變的事情 - 儘管它不是很正確 - 我因此就高調地回答了你的答案。 – mattosmat