1

我试图让 angular-ui ui-keypress 在按下向下键时调用一个函数。从我发现向下键是代码号 40。但是我无法触发事件。

如果您查看此 jsfiddle,它会触发 ENTER 按键上的功能(尝试输入或什至 13)。但是,如果您将其更改为 40,它将不起作用。http://jsfiddle.net/jayrmotta/NbjZL/70/

<!DOCTYPE html>
<html ng-app="myModule" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Main</title>
</head>
<body ng-controller="Ctrl">
  <button ng-click="add()">Add</button>
  <input type="text" ui-keypress="{40: 'add()'}" />
  {{item}}
</body>
</html>


var myModule = angular.module('myModule', ['ui.directives']);
function Ctrl($scope) {
  $scope.item = "";

  $scope.add = function () {
    $scope.item = "Item Added";
  }
}

我有错误的代码还是可以告诉我出了什么问题?

4

1 回答 1

8

JavaScriptkeypress事件不会针对上、下、左或右触发。您想使用keyuporkeydown代替。在你的情况下,你想要:ui-keyup="{40: 'add()'}"

http://www.w3schools.com/jsref/event_onkeypress.asp

注意:onkeypress 事件并非针对所有浏览器中的所有键(例如 ALT、CTRL、SHIFT、ESC)触发。要仅检测用户是否按下了某个键,请改用 onkeydown 事件,因为它适用于所有键。

于 2013-04-19T16:42:06.927 回答