Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从表中的其他td获取其他值_Javascript_Jquery_Html - Fatal编程技术网

Javascript 从表中的其他td获取其他值

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">

好的,我有一个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">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
属性上不匹配