我在表格中显示了一组搜索结果。每行都有一个单选框。一旦用户选择了一行,我想从相邻的单元格访问描述文本。
使用 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>
谢谢
最佳答案
$("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") );
});
关于javascript - 如何访问相邻表格单元格中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3699864/