admin管理员组文章数量:1323000
$("#showKey").each(
$(this).click(function(){
alert($(this).attr("value"));
})
);
And
<a id="showKey" href="#" value="{{ customer.key }}">
<span class="icons icon-key"></span>
Show key
</a>
The alert gives and undefined output, just 'undefined'. I have a list of customers and a click on #showKey should reveal the key for the clicked customer.
Whats wrong with my code?
$("#showKey").each(
$(this).click(function(){
alert($(this).attr("value"));
})
);
And
<a id="showKey" href="#" value="{{ customer.key }}">
<span class="icons icon-key"></span>
Show key
</a>
The alert gives and undefined output, just 'undefined'. I have a list of customers and a click on #showKey should reveal the key for the clicked customer.
Whats wrong with my code?
Share Improve this question asked May 10, 2012 at 20:08 DarkLeafyGreenDarkLeafyGreen 70.5k136 gold badges391 silver badges616 bronze badges 1-
Why do you use
each
on id selector? do you have multiple elements with the same id? It's an invalid HTML! – gdoron Commented May 10, 2012 at 20:21
4 Answers
Reset to default 6You cannot have multiple elements with the same ID - use a class instead. Additionally, you don't need the call to .each -- use a class selector instead:
$(".showKey").click(function(){
alert($(this).data("key"));
);
<a class="showKey" href="#" data-key="{{ customer.key }}">Show key</a>
you can use data
attribute:
<a id="showKey" href="#" data-value="{{ customer.key }}">
<span class="icons icon-key"></span>
Show key
</a>
$("#showKey").click(function(){
alert($(this).data("value"));
})
http://jsfiddle/LKArX/
You do not need the jQuery each function.
$("#showKey").click(function(){
alert($(this).attr("value"));
});
The problem with your code is in your usage of
$("#showkey").each({...});
You should simply use
$("#showkey").click({function(){
alert( $this).val() );
}
});
to bind the click event to every showkey id'd element.
本文标签: javascriptjQuery check click for each linkStack Overflow
版权声明:本文标题:javascript - jQuery check click for each link - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742115789a2421462.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论