【问题标题】:How to detect SHIFT key in jQuery keydown event handler?如何在 jQuery keydown 事件处理程序中检测 SHIFT 键?
【发布时间】:2013-04-18 01:49:13
【问题描述】:

我有以下 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 会导致输入星号 * 字符。似乎允许“8”键与 SHIFT 组合。如何防止 SHIFT+8 被接受并插入“*”字符?

【问题讨论】:

  • 这是一个布尔值e.shiftKey
  • 详细说明大卫的评论。您需要做的是测试是否 e.shiftKey=true 表示按下了 shift 键,然后返回 false。

标签: jquery events keyboard


【解决方案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

【讨论】:

    猜你喜欢
    • 2022-09-26
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多