jquery $(this).id 返回未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10578566/
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
jquery $(this).id return Undefined
提问by Mahdi_Nine
I have two asp radio buttons on a page that are placed in a updatepanel
. I wrote a click event for them with jQuery like this:
我在一个页面上有两个 asp 单选按钮,它们位于updatepanel
. 我用 jQuery 为他们写了一个点击事件,如下所示:
$(document).ready(function () {
$(".inputs").click(function () {
alert($(this).id);
});
});
But it returns Undefined. What is the problem?
但它返回未定义。问题是什么?
EDIT:
alert(" or " + $(this).attr("id"));
alert(this.id);
These two lines return null
!
这两行返回null
!
回答by Sampson
$(this)
and this
aren't the same. The first represents a jQuery object wrapped around your element. The second is just your element. The id
property exists on the element, but not the jQuery object. As such, you have a few options:
$(this)
并且this
不一样。第一个代表包裹在元素周围的 jQuery 对象。第二个只是你的元素。该id
属性存在于元素上,但不存在于 jQuery 对象上。因此,您有几个选择:
Access the property on the element directly:
this.id
Access it from the jQuery object:
$(this).attr("id")
Pull the object out of jQuery:
$(this).get(0).id; // Or $(this)[0].id
Get the
id
from theevent
object:When events are raised, for instance a click event, they carry important information and references around with them. In your code above, you have a click event. This event object has a reference to two items:
currentTarget
andtarget
.Using
target
, you can get theid
of the element that raised the event.currentTarget
would simply tell you which element the event is currently bubbling through. These are not always the same.$("#button").on("click", function(e){ console.log( e.target.id ) });
直接访问元素上的属性:
this.id
从 jQuery 对象访问它:
$(this).attr("id")
从 jQuery 中拉出对象:
$(this).get(0).id; // Or $(this)[0].id
id
从event
对象中获取:当引发事件时,例如单击事件,它们会携带重要的信息和参考。在上面的代码中,您有一个点击事件。这个事件对象有两个项目的引用:
currentTarget
和target
。使用
target
,您可以获得id
引发事件的元素的 。currentTarget
只会告诉您事件当前正在冒泡的元素。这些并不总是相同的。$("#button").on("click", function(e){ console.log( e.target.id ) });
Of all of these, the best option is to just access it directly from this
itself, unless you're engaged in a series of nested events, then it might be best to use the event
object of each nested event (give them all unique names) to reference elements in higher or lower scopes.
在所有这些中,最好的选择是直接从其this
自身访问它,除非您参与一系列嵌套事件,否则最好使用event
每个嵌套事件的对象(给它们所有唯一的名称)来更高或更低范围内的参考元素。
回答by Tieson T.
Another option (just so you've seen it):
另一种选择(就像你已经看到的那样):
$(function () {
$(".inputs").click(function (e) {
alert(e.target.id);
});
});
HTH.
哈。
回答by Tats_innit
Hiya demohttp://jsfiddle.net/LYTbc/
Hiya演示http://jsfiddle.net/LYTbc/
this is a reference to the DOM element, so you can wrap it directly.
这是对 DOM 元素的引用,因此您可以直接包装它。
attr
api: http://api.jquery.com/attr/
attr
api:http: //api.jquery.com/attr/
The .attr() method gets the attribute value for only the first element in the matched set.
.attr() 方法仅获取匹配集中第一个元素的属性值。
have a nice one, cheers!
有一个不错的,干杯!
code
代码
$(document).ready(function () {
$(".inputs").click(function () {
alert(this.id);
alert(" or " + $(this).attr("id"));
});
});?
});?
回答by mu is too short
$(this)
is a jQuery object that is wrapping the DOM element this
and jQuery objects don't have id
properties. You probably want just this.id
to get the id
attribute of the clicked element.
$(this)
是一个包裹 DOM 元素的 jQuery 对象,this
而 jQuery 对象没有id
属性。您可能只想this.id
获取id
被单击元素的属性。
回答by robert
use this actiion
使用这个动作
$(document).ready(function () {
var a = this.id;
alert (a);
});
回答by Ravi Gadag
this : is the DOM Element $(this) : Jquery objct, which wrapped with Dom Element, you can check this answer also this vs $(this)
this : is the DOM Element $(this) : Jquery objct, 它用 Dom Element 包裹,你也可以检查这个答案 this vs $(this)
try like this Attr(). Get the value of an attribute for the first element in the set of matched elements.
试试这个 Attr()。获取匹配元素集中第一个元素的属性值。
$(document).ready(function () {
$(".inputs").click(function () {
alert(" or " + $(this).attr("id"));
});
});