2013-06-01 46 views
0

我試圖得到一個錶行的ID與已知的類name.I正在使用AJAX獲取錶行的ID與partcular類

獲取數據...

$.get("getall.php", function(data){  
    { 
    $('.tableholder').append(data); 
    $('tr:first-child').addClass("current"); 

然後給第一子級電流和工作原理expected.However這將返回undefined

var idcell = $('table').find('tr.current').attr('id'); 
    alert(idcell); 

爲什麼會返回undefined?

+1

你確定它有一個ID?你使用鉻工具檢查過嗎? – writeToBhuwan

+0

我剛剛檢查過,它有點奇怪。它可以工作,如果我添加類像$('tr:first-child')。next()。addClass(「current」);但是會將該類添加到表格的第二行。 – Gandalf

+0

你可以嘗試像'$('。tableholder')。append(data); ('。tableholder tr:first-child')。addClass(「current」);'然後'var idcell = $('。tableholder tr.current')。attr('id'); alert(idcell);'once .. –

回答

0

我已經確定了problem.Instead做

$('tr:first-child').addClass("current"); 

的,我沒有

$('tr:first-child').next().addClass("current"); 

,現在的作品,但當前類適用於第二行。

編輯

到類電流添加到第一個行我用這個

$('tr:eq(1)').addClass("current"); 

我發現這個有用的https://stackoverflow.com/a/839212/1867062