2010-09-13 67 views
1

我有一組表格中顯示的搜索結果。每一行都有一個收音機。一旦用戶選擇了一行,我想從鄰居單元訪問描述文本。如何訪問鄰居表單元格中的文本?

使用jQuery或直接javascript,這樣做的最好方法是什麼?

<tr class="odd"> 
<td class="chosenCode"><input type="radio" value="123" name="chosenCode"></td> 
<td class="codeFound">123</td> 
<td class="descriptionFound">This is description text for code 123</td> 
</tr> 

感謝

回答

2
$("table input:radio").change(function() { 
    alert($(this).closest("tr").children(".descriptionFound").text()); 
}); 

,或者更復雜的:

// prepare once 
$("table input:radio").each(function() { 
    var descr = $(this).closest("tr").children(".descriptionFound").text(); 
    $(this).data("descr", descr); 
}); 

// use 
$("table input:radio").change(function() { 
    alert($(this).data("descr")); 
}); 
+0

大隻是我在找什麼,謝謝! – Chris 2010-09-13 11:34:11

1

裏面的事件回調函數可以使用此代碼來獲取描述元素的內容。

$(this).next('.descriptionFound').text(); 
+0

偉大的只是我在找什麼,謝謝! – Chris 2010-09-13 11:33:32

相關問題