Javascript 从表中的其他td获取其他值
好的,我有一个HTML表,在一个TR(tow)中有4个TDs,如下代码所示:Javascript 从表中的其他td获取其他值,javascript,jquery,html,Javascript,Jquery,Html,好的,我有一个HTML表,在一个TR(tow)中有4个TDs,如下代码所示: <table> <tr> <td class="1">Lemon</td> <td class="2">Orange</td> <td class="3">Tea</td> <td class="4"><a href=#" class="get">
<table>
<tr>
<td class="1">Lemon</td>
<td class="2">Orange</td>
<td class="3">Tea</td>
<td class="4"><a href=#" class="get">Get</a></td>
</tr>
<tr>
<td class="1">Apple</td>
<td class="2">Tomato</td>
<td class="3">Pineapple</td>
<td class="4"><a href="#" class="get">Get</a></td>
</tr>
</table>
关于我该怎么做或者我做错了什么有什么想法吗
谢谢 您在td
元素中调用.find()
,而实际上您需要在tr
中调用它,这一级别更高
将$(this.parent().find(…)
替换为$(this.parent().parent().find(…)
(正如pimvdb所建议的那样,您应该使您的ID唯一。)您正在调用.find()
在td
元素中,而实际上您需要在tr
中调用它,这一级别更高
将$(this.parent().find(…)
替换为$(this.parent().parent().find(…)
(正如pimvdb建议的那样,您应该使您的ID唯一。)
您应该使用唯一的id
,以下是修改后的代码:
$(document).ready(function(){
$('a.get').click(function(e){
e.preventDefault();
var val1 = $(this).closest('tr').find('td.1').html();
var val2 = $(this).closest('tr').find('td.2').html();
var val3 = $(this).closest('tr').find('td.3').html();
alert(val1 + val2 + val3);
});
});
使用parent
您将返回到td
,因为链接在其中,所以您需要返回到tr
,这是通过最近('tr')
完成的。此外,还修改了html,使link元素具有唯一的id
<a href=#" class="get">Get</a>
您应该使用唯一的id
,以下是修改后的代码:
$(document).ready(function(){
$('a.get').click(function(e){
e.preventDefault();
var val1 = $(this).closest('tr').find('td.1').html();
var val2 = $(this).closest('tr').find('td.2').html();
var val3 = $(this).closest('tr').find('td.3').html();
alert(val1 + val2 + val3);
});
});
使用parent
您将返回到td
,因为链接在其中,所以您需要返回到tr
,这是通过最近('tr')
完成的。此外,还修改了html,使link元素具有唯一的id
<a href=#" class="get">Get</a>
如果单元格只是数值型的,那么在单元格中添加类是没有意义的-您可以使用
eq()
在单元格中添加一个类是没有意义的,如果它们只是数字的,您可以使用
eq()
你已经用过两次身份证了。ID必须是唯一的,否则它不再是真正的ID。您的引号在
href
属性上不匹配您已经使用了两次ID。ID必须是唯一的,否则它不再是真正的ID。您的引号在href
属性上不匹配