javascript 获取keyup上输入的id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9575564/
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
Get the id of input on keyup
提问by Joe
I have an input box...
我有一个输入框...
<input type="text" id="search_member" onkeyup="lookup(this.value);">
When I type in the field, it will go to function lookup(). From there I want to get the id of this input. I tried...
当我在该字段中键入时,它将转到函数 lookup()。从那里我想获取此输入的 ID。我试过...
var This_id = $(this).attr("id");
but this won't work. Any suggestions on how I can get the id?
但这行不通。关于如何获取 id 的任何建议?
回答by
Because you are passing this.value
to your lookup()
function. It's better to pass this
to your function and then use arg.value
to get the value and arg.getAttribute('id')
for the id
因为你正在传递this.value
给你的lookup()
函数。最好传递this
给您的函数,然后用于arg.value
获取值并arg.getAttribute('id')
用于id
<input type="text" id="search_member" onkeyup="lookup(this);">
function lookup(arg){
var id = arg.getAttribute('id');
var value = arg.value;
// do your stuff
}
回答by amit_g
Get rid of onkeyup="lookup(this.value);
摆脱 onkeyup="lookup(this.value);
<input type="text" id="search_member">
and then use
然后使用
$(function(){
$("#search_member").keyup(function(e){
var This_id = $(this).attr("id");
});
};
回答by Stuart Bentley
$(document).keyup(function(e) {
if (e.currentTarget.activeElement != undefined) {
var id = $(e.currentTarget.activeElement).attr('id');
}
});
回答by Jivings
The problem there is you're not passing the element in, you're passing the value. You need to change it to this:
问题是你没有传入元素,而是传入了值。你需要把它改成这样:
<input type="text" id="search_member" onkeyup="lookup(this);">
Then the rest of your code should work fine.
然后你的其余代码应该可以正常工作。
回答by androidavid
why dont you give the function lookup the THIS?
你为什么不给函数查找这个?
lookup(this);
then u can also use the code you suggested
那么你也可以使用你建议的代码