[英]Get ID by Element Class
I have several text boxes which have drop downs on them. 我有几个文本框,上面有下拉列表。 When a user clicks on the field with the drop down class, I want to get the ID of that field. 当用户单击带有下拉菜单的字段时,我想获取该字段的ID。 This seems like it should be simple, but I can't quite seem to figure it out... 这似乎很简单,但我似乎不太清楚...
I have tried: 我努力了:
$(".dropdown").click(function () {
alert(this.getElementById);
});
Yet this only returns undefined. 但这只会返回未定义的。
ID is a property of the object, so just use following code: ID是对象的属性,因此只需使用以下代码:
$(".dropdown").click(function () {
alert(this.id);
});
Wrong syntax.You have to write 语法错误。您必须编写
$(".dropdown").click(function () {
alert(this.id);
});
var idStr = element.id; // Get the id.
element.id = idStr; // Set the id
You're using getElementById
incorrectly. 您使用的getElementById
错误。 It's a function used to get a dom element on your page using an id as filter condition. 该函数用于使用id作为过滤条件在页面上获取dom元素。 It's not used to get element id. 它不用于获取元素ID。
Correct usage of getElementById
is 正确使用getElementById
是
var elem = document.getElementById('theid');
You simply have to use 您只需要使用
alert(this.id);
If I understood well, you want this: 如果我了解得很好,那么您需要这样做:
$(".dropdown").click(function () {
alert($(this).attr('id'));
});
correct me if I am wrong 如果我错了请纠正我
Its simple you can alert the element id with the below code written by me 很简单,您可以使用我编写的以下代码来提醒元素ID
$(".dropdown").click(function() {
var id = $('.dropdown').attr('id');
alert(id);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.