my code is :-
for(i = 1; i <= 4; i++)
{
$('.user_list').append('<li><input type="button" class="user'+ i + '"/></li&开发者_Go百科gt;');
}
which gives output:-
<li><input type="button" class="user_1" /><li>
<li><input type="button" class="user_2" /><li>
<li><input type="button" class="user_3" /><li>
<li><input type="button" class="user_4" /><li>
can you tell me what code to write so that when j-query runs and i click any one the buttons and get the corresponding id for the buttons
to alert id...
$(function(){
$("input").click(function(){
alert($this.attr('id'));
});
});
But you have not set id of any of the buttons, so it'll alert with blank message.
try this to get the class
$("input[type='button']").click(function() {
alert($(this).attr("class"));
});
http://jsfiddle.net/esYAh/
You mean class probably
$(function(){
$("input").click(function(){
alert($(this).attr('class'));
});
});
demo
精彩评论