如何检测jQuery keydown事件处理程序中的SHIFT键?

时间:2013-04-24 13:23:43

标签: jquery events keyboard

我有以下jQuery函数

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并插入“*”字符?

1 个答案:

答案 0 :(得分:-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>

以关键字&GT; event.shiftKey

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