【问题标题】:How to access text from neighboring table cell?如何访问相邻表格单元格中的文本?
【发布时间】:2010-09-13 19:19:59
【问题描述】:
我有一组显示在表格中的搜索结果。每行都有一个单选框。一旦用户选择了一行,我想从相邻单元格中访问描述文本。
使用 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>
谢谢
【问题讨论】:
标签:
javascript
jquery
html
xhtml
jquery-selectors
【解决方案1】:
$("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") );
});
【解决方案2】:
在事件回调函数内部,您可以使用此代码获取描述元素的内容。
$(this).next('.descriptionFound').text();