jQuery-获取与单击元素相同的行中的表格单元格的文本值 [英] jQuery - Getting the text value of a table cell in the same row as a clicked element
问题描述
我单击表格单元格中的链接.我需要获取同一表行中特定单元格的值.
I click a link in a table cell. I need to get the value of a specific cell within this same table row.
<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"的值.
I have a click handler attached to the link in the fourth cell. That click handler calls a function that opens a modal window. When a form in the modal is submitted I want to also pass the value of td class="two" from the row in which the link was clicked to this modal.
这是发送模态的函数(问题区域正在为var Something获取正确的值):
Here is the function that sends the modal (the problem area is getting the correct value for 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的值.
The problem is that I cannot get the correct value for Something. It should be the value of td class=two in the same row as the clicked element.
这一切融合在一起的方式是.单击目标链接,该链接将调用一个名为Send_Click()的方法. Send_Click会进行一些验证,然后调用Send(),但从不填充Something的值.这是因为this
不是我想的吗?哎呀!
The way this all comes together is. Click the target link, that calls a method called Send_Click(). Send_Click does some validations and then calls Send() but the value for Something is never populated. Is this because this
is not what I'm thinking it is? Hjelp!
推荐答案
您想要.children()
代替(此处的文档):
$(this).closest('tr').children('td.two').text();
这篇关于jQuery-获取与单击元素相同的行中的表格单元格的文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!