[英]How to identify the input in which the key was pressed in JS code
I use the following jQuery code to process keys which are pressed in various input tags: 我使用以下jQuery代码来处理在各种输入标签中按下的键:
$(document).ready( function () {
$("input").keydown(function (e) {processKeys(e);});
...
It works great... 效果很好...
In a separate Javascript file I have a function which receives the event call: 在一个单独的Javascript文件中,我有一个函数来接收事件调用:
function processKeys(e) {
key=e.which;
if (key==27) {
$("#searchCDB").hide();
}
}
So, is there a way for me to identify the <input>
tag which caused the event at the event layer... What I mean is here, in some way like: 因此,有没有办法让我在事件层识别导致事件发生的
<input>
标签...我的意思是在这里,以某种方式:
$("input").keydown(function (e) {processKeys($("#this"),e);});
I know my attempt is absurd, but any suggestion that works will be appreciated. 我知道我的尝试是荒谬的,但是任何可行的建议都会受到赞赏。
DK DK
You can pass in this
to your processKeys
function: 你可以通过
this
您processKeys
功能:
$(document).ready( function () {
$("input").keydown(function (e) {
processKeys(e, this);
});
});
function processKeys(e, obj) {
console.log(obj.id); //logs ID of keypressed input
key=e.which;
if (key==27) {
$("#searchCDB").hide();
}
}
Demo: http://jsfiddle.net/uE7ZD/ 演示: http : //jsfiddle.net/uE7ZD/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.