简体   繁体   English

如何访问相邻表格单元格中的文本?

[英]How to access text from neighboring table cell?

I have a set of search results presented in a table. 我在表格中显示了一组搜索结果。 Each row has a radio box. 每行都有一个单选框。 Once the user has selected a row I would like to access the description text from the neighboring cell. 一旦用户选择了一行,我想从相邻的单元格访问描述文本。

Using jQuery or straight javascript, what is the best way to do this? 使用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>

Thanks 谢谢

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

Or, more elaborate: 或更详细地说:

// 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") );
});

在事件回调函数内部,您可以使用此代码获取description元素的内容。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM