jQuery Code on <td> with ajax to extract a column from a table
I have a table of banned IPs. The first column holds the IPs, the second one has the timestamp and the third the action links. I want to make an ajax call when "Remove Ban" is clicked on the link. For that I need the IP address from the first column. But somehow, this isn't working. Here 开发者_StackOverflow中文版are the codes (You can check it on jsfiddle.net)
<tr>
<td class="ip">192.168.1.1</td>
<td class="centered">August 10, 2011</td>
<td class="right action_td">
<a class="action ban remove" href="javascript://">Remove Ban</a>
</td>
</tr>
(This is just a <tr>
. Other elements of the table removed.)
The js:
$(".action.ban.remove").live('click', function(){ // remove ban
ip = $(this).parent("td.right").siblings("td.ip").html();
alert(ip);
});
Instead of 192.168.1.1, it alerts with "null". Am I doing it wrong?
update okay, I figured out the problem. I had another handler attached to it which was creating the problem. Foolish of me. :-\
Change it to:
ip = $(this).parent().siblings("td.ip").text();
http://jsfiddle.net/sRRn9/
Just needed a table element to wrap the table row. See here.
$('.remove').click(function(e){
e.preventDefault();
alert($(this).parent().prev().prev().html());
});
http://jsfiddle.net/6VFm5/
Try this
$(".remove").live('click', function(){ // remove ban
ip = $(this).parent("td.right").siblings("td.ip").html();
alert(ip);
});
Working demo
精彩评论