我創建一個表與2行的PHP文件和一個按鈕上的每個最後<td>
獲得TD的價值:嘗試當我按下一個按鈕
<table id="filterTableMsg" border="1" class="TF">
<tbody><tr><th>Message Title</th><th>Message</th><th>Schedule Date</th><th>share</th> </tr>
<tr class="row_hover">
<td>hello</td>
<td>hello message</td>
<td>2014-04-09 00:11:51</td>
<td><button id="FBshare">share it!</button></td>
</tr>
<tr class="row_hover">
<td>hello again</td>
<td>hello new message</td>
<td>2014-04-08 10:15:21</td>
<td><button id="FBshare">share it!</button></td>
</tr>
</tbody>
</table>
當我按下一個按鈕(id="FBshare"
)我需要獲取行索引,然後使用jQuery獲取cell [0],cell [1]和cell [2]的值。
我嘗試用這個代碼來解決這個問題,但它返回「未定義」:
$(function() {
/*when i click on the FBshare button*/
$(document).on('click', '#FBshare', function(e){
e.preventDefault();
var mytable = $('#filterTableMsg tr:eq(1) td');//for example to get the 2nd row
alert(mytable.eq(2).html());//here i try to get the html of the 2nd cell but i get 'undifined
});
我可以通過編寫此獲得按鈕的行的索引:
alert($(this).closest('tr').index()); // but I can't make them work all together.
有沒有人面臨相同或相似的問題?
請舉個手
一個問題我看到的是,你有相同的ID(FBshare)多個元素。 Id的需要是獨一無二的。 – Khan