当用户点击表格行末尾的按钮时,我需要一些帮助从表中提取单元格数据。该表是通过Ajax技术生成的。我不确定在访问表格元素时这是否会有所不同。
<script type='text/javascript' src='jquery.js'></script>
<p>** click me **</p>
<br/>
<div id="placeHolder">some text goes here!</p>
<script>
$("p").click(function () {
$.ajax({
url: "status_2.html",
cache: false,
success: fnSuccess
}
);
});
function fnSuccess(msg)
{
$('div#placeHolder').html(msg);
}
function selectEditActivity(pass_id){
var row = $('#'+pass_id);
var seq = row.attr('seq');
// here I would like to pull the value of the date from the selected row
// but I just can't get syntax right. I get a null value on alert
var from_date = $('td', row).eq(0).html();
alert(from_date);
}
</script>
这是读入的html代码(file:status_2.html)
<span id="status2">
<span id="response" class="response"></span>
<table class="tableBorder">
<tr >
<th class="align_left">Date</th>
</tr><tr>
<tr seq="1" class="row_theme2" id="id3014201105191"></tr>
<tr><td>20090519</td>
<td>1330</td>
<td>1430</td>
<td>LUNCH</td>
<td></td>
<td><a onclick="selectEditActivity('id3014201105191');" href="#">EDIT</a></td>
</tr>
<tr seq="2" class="row_theme2" id="id3014201105192"></tr>
<tr><td>20100519</td>
<td>1330</td>
<td>1430</td>
<td>LUNCH</td>
<td></td>
<td><a onclick="selectEditActivity('id3014201105192');" href="#">EDIT</a></td>
</tr>
<tr seq="3" class="row_theme2" id="id3014201105193"></tr>
<tr><td>20110519</td>
<td>1330</td>
<td>1430</td>
<td>LUNCH</td>
<td></td>
<td><a onclick="selectEditActivity('id3014201105193');" href="#">EDIT</a></td>
</tr>
</tbody></table>
答案 0 :(得分:2)
改变这个:
var from_date = $('td', row).eq(0).html();
到此:
var from_date = row.next().children('td').eq(0).html();
...因为您要查找的信息位于具有ID的下一行。具有ID的行没有<td>
个元素。
直播示例: http://jsfiddle.net/kkP56/
编辑:您可以摆脱内联的onclick处理程序,让jQuery根据需要分配处理程序。
$(function() {
$('table tr a').click(function() {
var from_date = $(this).parent().prevAll().last().html();
alert( from_date );
});
});