I have a table which displays the Dynamic rows from server side script .Each rows conatins various values and the first row value contains the link such as "Show/Hide" when we click on "show" it shows the sub rows and when clicked on "hide" it hides the rows. Now the "Show/hide" is dynamic is such way their id is .where $i is dynamic value which takes (0,1,2..so on) Now how do we handle the click of "Show/hide" for each row
var j=0;
$("#mylink"+j).click(func开发者_如何学运维tion(){
})
//In the above statemnt I can handle only "0th" link and how do we handle the links for 1 ,2 and so on.........
Instead of an ID use a class, for example:
<a class="mylink" href="something.html">My Link</a>
Then use .live()
instead of .click()
, like this:
$(".mylink").live('click', function(){
//do something, e.g. $(this).closest('tr').something();
});
.live()
will listen for events from elements regardless of when there were added because the events bubble up to document
by default. .click()
is actually binding a click handler to the elements it found at the time, so doesn't work for future elements.
精彩评论