gpt4 book ai didi

javascript - jquery 获取上一个元素的内容

转载 作者:行者123 更新时间:2023-11-28 00:31:23 28 4
gpt4 key购买 nike

我将这个结构放入我的 html 中。

<table border="1" cellpadding="1" cellspacing="2">
<thead>
<tr>
<th>Name</th><th>Age</th><th>Address</th><th>Option</th>
</tr>
</thead>
<tbody>
<tr>
<td>here is the name</td>
<td>here is the age</td>
<td>here is the address</td>
<td><button class="update">update</button></td>
</tr>
<tr>
<td>here is the name2</td>
<td>here is the age2</td>
<td>here is the address2</td>
<td><button class="update">update2</button></td>
</tr>
</tbody>
</table>

从上面的结构可以看出,显示效果是这样的。

table header = Name, Age, Address, Option.
table body = here is the name, here is the age, here is the address, button update
table body = here is the name2, here is the age2, here is the address2, button update2

现在我想做的是,当我单击更新按钮时,我必须能够获取地址 td 的内容以在同一行中命名 td,并且每个内容都必须存储在指定的变量中,例如($姓名,$年龄,$地址)。假设我有多行,我必须能够仅检索地址 td 的内容以命名同一行的 td 。更准确地说,正如上面的结构,我必须能够获取“这是姓名”、“这是年龄”、“这是地址”并将每个内容放入指定的变量中,例如($name, $age, $address) 当我点击“update”按钮而不是“update2”按钮时。

到目前为止我尝试过的是。

$('.update').click(function(){
$address = $(this).prev('td').content();

alert ($dateposted);

});

但似乎没有返回任何东西。任何帮助将不胜感激。谢谢!

最佳答案

这里this是按钮,它没有前一个兄弟,您要查找的tdtd的前一个兄弟> 单击按钮的父级,您还必须使用 .text()/.html()获取内容

$address = $(this).parent().prev('td').text();//$(this).closest('td')

关于javascript - jquery 获取上一个元素的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28954727/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com