I have a list of div with the same class name. I wonder how to catch exactly which div client click on it by using jquery. I try many way in many times but I couldn''t figure out. Please help me because I''m newbie in jquery.
我有一个具有相同类名的div列表。我想知道如何使用jquery准确捕获哪个div客户端点击它。我多次尝试很多次,但我无法弄清楚。请帮帮我,因为我是jquery的新手。
2 个解决方案
#1
Use "this" in your click callback:
在点击回调中使用“this”:
$("div.nameofclass").click(function(){$(this).css('border', '1px solid red')});
$(“div.nameofclass”)。click(function(){$(this).css('border','1px solid red')});
#2
$('.myClass').click(function(){
$(this); //This is the clicked div
});
Edit: In response to your new question
编辑:回答您的新问题
You can get all the radio button selected doing something like this:
您可以选择所有单选按钮,执行以下操作:
$('input:radio:checked').each(function(){
$(this); // Checked radio button element(s)...
});
There you can access the element value with $(this).val()
, or also the name attribute, with $(this).attr('name')
.
在那里,您可以使用$(this).val()或name属性访问元素值,使用$(this).attr('name')。
Recommended lecture:
#1
Use "this" in your click callback:
在点击回调中使用“this”:
$("div.nameofclass").click(function(){$(this).css('border', '1px solid red')});
$(“div.nameofclass”)。click(function(){$(this).css('border','1px solid red')});
#2
$('.myClass').click(function(){
$(this); //This is the clicked div
});
Edit: In response to your new question
编辑:回答您的新问题
You can get all the radio button selected doing something like this:
您可以选择所有单选按钮,执行以下操作:
$('input:radio:checked').each(function(){
$(this); // Checked radio button element(s)...
});
There you can access the element value with $(this).val()
, or also the name attribute, with $(this).attr('name')
.
在那里,您可以使用$(this).val()或name属性访问元素值,使用$(this).attr('name')。
Recommended lecture: