jquery <a> 标签点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4294484/
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 <a> tag click event
提问by Maverick
I am building a code which displays user information on search. User information, is then displayed in a fieldset
, and a image, first name, last name and few profile info. is shown, and in the bottom of the fieldset
, there's a add as friend hyperlink:
我正在构建一个代码,用于显示用户搜索信息。用户信息然后显示在fieldset
, 和图像中,名字,姓氏和一些个人资料信息。显示,在底部fieldset
,有一个添加为朋友的超链接:
<a href="#" id="aaf">add as friend</a>
Now I want to use jquery $post()
method to interact with another page. I also have a hidden field inside that user fieldset
which has the user id value. Now, when i create a click functionality using jquery
, i am not able to access the different hidden field values. Now i want to know how to achieve this functionality? For checking if i can get the value of hidden fields inside a set of code, i did this.
现在我想使用 jquery$post()
方法与另一个页面进行交互。我在该用户中还有一个隐藏字段,fieldset
该字段具有用户 ID 值。现在,当我使用 来创建点击功能时jquery
,我无法访问不同的隐藏字段值。现在我想知道如何实现这个功能?为了检查我是否可以获得一组代码中隐藏字段的值,我这样做了。
$(document).ready(function () {
$("a#aaf").bind('click', function () {
alert($("#uid").val());
});
});
But I'm only getting the value of first fieldset
, not others. Kindly guide me in this.
但我只得到 first 的价值fieldset
,而不是其他人。请指导我。
Thanks in advance.
提前致谢。
EDIT: How to get it in each tag click event? I'm putting some more code here,
编辑:如何在每个标签点击事件中获取它?我在这里放了一些代码,
<?php foreach($query->result() as $row){?>
<fieldset>
<legend>
<?php echo $row->firstname.' '.$row->lastname;?>
</legend>
<img src='<?php echo $row->profile_img_url;?>'/><br>
<a href="#" id="aaf">add as friend</a>
<input name="uid" type="hidden" value='<?php echo $row->uid;?>' id="uid">
</fieldset>
回答by gfivehost
<a href="javascript:void(0)" class="aaf" id="users_id">add as a friend</a>
on jquery
在 jquery 上
$('.aaf').on("click",function(){
var usersid = $(this).attr("id");
//post code
})
//other method is to use the data attribute
//另一种方法是使用data属性
<a href="javascript:void(0)" class="aaf" data-id="102" data-username="sample_username">add as a friend</a>
on jquery
在 jquery 上
$('.aaf').on("click",function(){
var usersid = $(this).data("id");
var username = $(this).data("username");
})
回答by karim79
That's because your hidden fields have duplicate IDs, so jQuery only returns the first in the set. Give them classes instead, like .uid
and grab them via:
那是因为你的隐藏字段有重复的 ID,所以 jQuery 只返回集合中的第一个。改为给他们上课,喜欢.uid
并通过以下方式获取它们:
var uids = $(".uid").map(function() {
return this.value;
}).get();
Demo: http://jsfiddle.net/karim79/FtcnJ/
演示:http: //jsfiddle.net/karim79/FtcnJ/
EDIT: say your output looks like the following (notice, IDs have changed to classes)
编辑:假设您的输出如下所示(注意,ID 已更改为类)
<fieldset><legend>John Smith</legend>
<img src='foo.jpg'/><br>
<a href="#" class="aaf">add as friend</a>
<input name="uid" type="hidden" value='<?php echo $row->uid;?>' class="uid">
</fieldset>
You can target the 'uid' relative to the clicked anchor like this:
您可以像这样定位相对于点击的锚点的“uid”:
$("a.aaf").click(function() {
alert($(this).next('.uid').val());
});
Important: do not have any duplicate IDs. They will cause problems. They are invalid, badand you should notdo it.
重要提示:不要有任何重复的 ID。它们会引起问题。他们是无效的,坏的,你不应该这样做。
回答by Stephen Curial
All the hidden fields in your fieldset are using the same id, so jquery is only returning the first one. One way to fix this is to create a counter variable and concatenate it to each hidden field id.
字段集中的所有隐藏字段都使用相同的 id,因此 jquery 只返回第一个。解决此问题的一种方法是创建一个计数器变量并将其连接到每个隐藏字段 ID。