2013-04-24 2 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을 누르면 별표 (*) 문자가 입력된다는 것입니다. SHIFT와 함께 "8"키가 허용됩니다. SHIFT + 8이 허용되지 않고 "*"문자가 삽입되는 것을 방지하려면 어떻게해야합니까?

+7

은 다윗의 코멘트에 자세히 설명하기 위해 부울'e.shiftKey' –

+1

입니다. e.shiftKey = true가 Shift 키가 눌 렸음을 나타내는 지 테스트 한 다음 false를 반환하면됩니다. – ChrisP

답변

-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

관련 문제