javascript 如何在该文本框的 keydown 事件上获取文本框的 id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16215399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the id of the textbox on keydown event of that textbox
提问by Xavier
I have a text box in my page:
我的页面中有一个文本框:
<td>
<input type="text" id="Approvers1" size="11" onkeydown="AddUsersKeyDown(event)" style="width: 470px;" />
</td>
This is my js function:
这是我的 js 函数:
function AddUsersKeyDown(evtobj) {
if (evtobj.keyCode == 75 && evtobj.ctrlKey) {
AddUsers(--input parameter--);
}
}
Now if i press ctrl k in my textbox it must call another javascript function named AddUsers(--input parameter--).. where the input parameter should be the id of the textbox...
现在,如果我在文本框中按 ctrl k,它必须调用另一个名为 AddUsers(--input parameter--).. 的 javascript 函数,其中输入参数应该是文本框的 id...
How to achieve this.. Either i need the id of the textbox or atleast the DOM object so that i can access the id of textbox from DOM object..
如何实现这一点.. 我需要文本框的 id 或至少 DOM 对象,以便我可以从 DOM 对象访问文本框的 id..
Kindly help me in achieving either one of the two things..
请帮助我实现这两件事中的任何一件。
回答by TryingToImprove
First you should rename the function to AddUsersKeyDown
so the keydown event will be triggered. On the event object, there is a target
property, which have the triggering DOM element. You can use that information to get the id.
首先,您应该将该函数重命名为,AddUsersKeyDown
以便触发 keydown 事件。在事件对象上,有一个target
属性,它具有触发 DOM 元素。您可以使用该信息来获取 ID。
<input type="text" id="Approvers1" size="11" onkeydown="AddUsersKeyDown(event)" style="width: 470px;" />
And the Javascript:
和 Javascript:
function AddUsersKeyDown(evtobj) {
var target = evtobj.target || evtobj.srcElement;
if (evtobj.keyCode == 75 && evtobj.ctrlKey) {
AddUsers(target.id);
return false;
}
}
Here is a jsfiddle: http://jsfiddle.net/q26Nv/3/
这是一个 jsfiddle:http: //jsfiddle.net/q26Nv/3/
回答by Prasath K
Use this
to get the DOM element or this.id
to get the id of the element
使用this
得到的DOM元素或this.id
获取该元素的id
<td>
<input type="text" id="Approvers1" size="11" onkeydown="AddUsersKeyDown(event,this.id)" style="width: 470px;" />
</td>
function AddUsersKeyDown(evtobj,id) {
if (evtobj.keyCode == 75 && evtobj.ctrlKey) {
AddUsers(id);
}
}
回答by picios
get jqueryhere
在这里获取 jquery
and then take of onkeydown
然后取onkeydown
<input type="text" id="Approvers1" class="keydown" size="11" style="width: 470px;" />
add script
添加脚本
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
$(function() {
$('.keydown').keydown(function() {
console.log( $(this).attr('id') );
});
});
<script>
maybe a lot of code, but you can be sure it's cross browsers documentation
也许有很多代码,但你可以确定它是跨浏览器 文档
回答by Alex K.
You can:
你可以:
function AddUserKeyDown(evtobj) {
evtobj = evtobj || window.event;
var target = evtobj.target || evtobj.srcElement;
if (evtobj.keyCode == 75 && evtobj.ctrlKey) {
AddUsers(target.id);
}
}
(Your func names don't match btw)
(顺便说一句,您的 func 名称不匹配)