微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jQuery – 获取与单击的元素在同一行中的表单元格的文本值

我单击表单元格中的链接。我需要获取在同一个表行中的特定单元格的值。
<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 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
    });        
};

问题是,我无法获得正确的值的东西。它应该是与clicked元素在同一行中的td class = two的值。

这一切都在一起的方式是。单击目标链接调用一个名为Send_Click()的方法。 Send_Click进行一些验证,然后调用Send(),但Something的值从未填充。这是因为这不是我想的是什么? Hjelp!

解决方法

你想要.children()而不是( documentation here):
$(this).closest('tr').children('td.two').text();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐