jquery在ng-repeat中找不到特定的元素



从以下html代码开始

<div ng-repeat="item in list">
<input type="text" class="texter">
</div>

我使用jquery来监听对列出的任何输入的按键。

$('.texter').keypress(function(e){
console.log(this);
}

但什么也没发生。即使使用

$('input').keypress(function(e){
console.log(this);
}

基本上,我想要获得触发keypress事件的元素。

有没有办法用jquery获得它?

请注意,在提问之前我已经搜索了答案,但似乎没有一个能达到我的预期。

为什么要使用jQuery绑定事件侦听器?通过将ng-keypress添加到ng-repeat中的输入中,可以使用Angular正确地执行此操作

角度

HTML

<div ng-repeat="item in list">
<input type="text" class="texter" ng-keypress="keypress($event)">
</div>

JS-

angular
.module("myApp", [])
.controller('myController', ['$scope', function($scope) {
$scope.list = [1,2,3];
$scope.keypress = function(event){
console.log(event);
}
}]);

演示

如果您坚持使用jQuery处理事件绑定,则应该使用$(document).on('keypress', ...)

$(document).on('keypress', '.texter', function(e){
console.log(e);
});

最新更新