2013-04-24 65 views
4

我有以下的jQuery函數如何檢測jQuery keydown事件處理程序中的SHIFT鍵?

jQuery.fn.integerMask = 
function() { 
return this.each(function() { 
    $(this).keydown(function (e) { 
    var key = (e.keyCode ? e.keyCode : e.which); 
    // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY 
    return (
       key == 8 || 
       key == 9 || 
       key == 46 || 
       key == 37 || 
       key == 39 || 
       (key >= 48 && key <= 57) || 
       (key >= 96 && key <= 105)); 
      ); 
     }); 
    }); 
    }; 

它用於數字輸入。問題在於SHIFT + 8會導致輸入星號*字符。它顯示組合鍵「8」與SHIFT被允許。我將如何防止接受SHIFT + 8並插入「*」字符?

+7

這是一個布爾'e.shiftKey' – 2013-04-24 13:32:04

+1

爲了詳細說明大衛的評論。你需要做的是測試e.shiftKey = true是否表示按下shift鍵然後返回false。 – ChrisP 2013-04-24 16:12:52

回答

-1
<!DOCTYPE html> 
<html> 
<head> 
<script> 
function isKeyPressed(event) 
{ 
if (event.shiftKey==1) 
    { 
    alert("The shift key was pressed!"); 
    } 
else 
    { 
    alert("The shift key was NOT pressed!"); 
    } 
} 
</script> 
</head> 

<body onmousedown="isKeyPressed(event)"> 

<p>Click somewhere in the document. An alert box will tell you if you pressed the shift key or not.</p> 

</body> 
</html> 

關鍵字定位> event.shiftKey

來源:http://www.w3schools.com/jsref/tryit.asp?filename=try_dom_event_shiftkey

相關問題