2010-11-16 57 views
2

對於需要傳遞行中前兩個tds內容的錶行,我有點擊函數。jQuery:在一行的onclick函數中指定兒童的索引​​

// Assign a click handler to the rows 
$('#supportTables1 tr').click(function() 
{ 
    var firstTd = $(this).find('td').html(); 
    //need to get the next td. below line doesn't work. 
    var secondTd = $(this).find('td')[1].html(); 
    window.open('snglContactList.php'+'?search_word='+firstTD+'?list_name=secondTd); 
}); 

我可以拿到首個TD就好了,但我難倒,我知道什麼是訪問的第二個TD(我試過設置它的指數diferrent方法)的深刻簡單的事情。

+0

你可以發佈html到這個? – shoebox639 2010-11-16 20:18:24

回答

5

數組符號[1]返回一個DOM元素,而不是一個jQuery對象,因此您不能在其上使用.html()(一種jQuery方法)。使用jQuery函數.eq()得到的元素來代替:

// Assign a click handler to the rows 
    $('#supportTables1 tr').click(function() 
    { 
     var firstTd = $(this).find('td').eq(0).html(); 
     var secondTd = $(this).find('td').eq(1).html(); 
     window.open('snglContactList.php'+'?search_word='+firstTD+'?list_name='+secondTd); 
    }); 
+0

完美運作。非常感謝! – matt 2010-11-16 22:35:33

0

錶行都有自己的這是由cells屬性來訪問它們所包含的細胞收集。

這樣做:

var firstTd = this.cells[ 0 ].innerHTML; 
var secondTd = this.cells[ 1 ].innerHTML; 

,或者你堅持一個jQuery的解決方案,這樣做:

var firstTd = $(this.cells[ 0 ]).html(); 
var secondTd = $(this.cells[ 1 ]).html(); 

例子:http://jsbin.com/ivabu4/

注意,第一個版本是最快的,因爲它直接訪問屬性LY。

0

$(this).find('td')將返回點擊行內的所有<td>。您可以使用EQ(IDX)方法在特定索引獲得的:

$('#supportTables1 tr').click(function() 
{ 
    var tds = $(this).find('td'); 
    var firstTd = tds.eq(0).text(); 
    var secondTd = tds.eq(1).text(); 
    window.open('snglContactList.php'+'?search_word='+firstTD+'?list_name='+secondTd); 
}); 

工作示例這裏(與警告,而不是window.open ...):

http://jsfiddle.net/E5Z5V/