我单击表格单元格中的链接。我需要获取同一表格行中特定单元格的值。
<tr>
<td class="one">this</td>
<td class="two">that</td>
<td class="three">here</td>
<td class="four"><a href="#">there</a></td>
</tr>
<tr>
<td class="one">qwqw</td>
<td class="two">dfgh</td>
<td class="three">ui</td>
<td class="four"><a href="#">there</a></td>
</tr>
我有一个点击处理程序附加到第四个单元格中的链接。该单击处理程序调用一个打开模式窗口的函数。当提交模式中的表单时,我还想将 td class="two"的值从单击链接的行传递到此模式。
这是发送模态的函数(问题区域是获取 var Something 的正确值):
var Send = function() {
var Name = $( '#name' ).val();
var Something = $(this).closest('td').siblings('.two').text(); // version 1. doesn't work
var Something = $(this).closest('tr').siblings('td.two').text(); // version 2 also doesn't work
var Something = $(this).attr('class'); // version 3. just a test but also doesn't work
$.ajax( {
async: false,
data: { name: Name, somedata: Something },
type: 'POST',
url: the url
});
};
问题是我无法获得 Something 的正确值。它应该是与被点击元素同一行的 td class=two 的值。
这一切结合在一起的方式是。单击目标链接,该链接将调用名为 Send_Click() 的方法。 Send_Click 执行一些验证,然后调用 Send(),但永远不会填充 Something 的值。这是因为 this
不是我想的那样吗?谢尔普!
最佳答案
您需要使用.children()
(documentation here):
$(this).closest('tr').children('td.two').text();
关于jQuery - 获取与单击的元素位于同一行的表格单元格的文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2315600/