简体   繁体   English

使用jQuery从表的最后一个tr获取属性

[英]Getting a attribute from the last tr of a table using jquery


Got stuck a bit in this jquery script.I want to get the link attribute of the last td in the tr on check of the check box. 在这个jquery脚本中有点卡住了。我想在选中复选框时获得tr最后一个tdlink属性。 Here is the HTML 这是HTML

      <tr>
          <td><input type="checkbox" name="check" value="happy"></td>
          <td style="width: 310px; line-height: 14px;" class="fileName <?php echo $myClass; ?>" ><div style="width: 310px; overflow: hidden;"><a href="https://abcd.com/<?php echo $info."/".$myinfo['name'];?>" title="<?php echo $myinfo['name'];?>" target="_blank"><?php echo $fileName.'.'.$fileExtension; ?></a></div></td>
           <td style="width: 150px;"><?php echo $fileSize; ?> KB</td>
           <td style="width: 150px;"><?php echo $fileModified; ?></td>
           <td style="width: 150px;">
               <a href="javascript:void(0);" ><span id="" link="https://abcd.com/<?php echo $info."/".$myinfo['name'];?>" title="<?php echo $myinfo['name'];?>" class="shareIcon"></span></a>
               <a href="javascript:void(0);"><span id="deleteIcon" link="<?php echo $myBucket['name'];?>" class="deleteIcon"></span></a>
           </td>
         </tr>

I want to get each of the link attribute in the last td when more than one checkbox are. 当多个复选框选中时,我想在最后一个td中获取每个link属性。
What I tried is this 我试过的是

   $("input[name='check']").on('change',function() {
   var checked = $("input[name='check']:checked");
   // console.log(checked);
   $.each(checked,function(){ 
   //console.log(checked.length); 
   if(checked.length>1){
          //  console.log('more than 1');             
          //console.log(checked.parents('td').siblings('.shareIcon'));
          checked.parents('td').siblings('.shareIcon'),function(){
          console.log($(this).attr('link'));

          }      
      } 
   })
 });

There are several mistakes: 有几个错误:
1) Validate your html, improper closing of input tag. 1)验证您的html, input标签的关闭不正确。 This leads the following 这导致以下

checked.parent()  //points the body not its actual td

2) Proper way of escaping / characters. 2)正确的转义方式/字符。

link="https://abcd.com/<?php echo $info."/".$myinfo['name'];?>"  //WRONG

3) The below code will point only td parent of input , so it fails 3)以下代码将仅指向input td父级,因此失败

checked.parents('td').find('.shareIcon').attr('link')

Finally, there is no need to iterate inorder to get the last td you can use :last like 最后,不需要迭代就可以使用:last一个td :last like

 $("input[name='check']").on('change',function() {
   var checked = $("input[name='check']:checked");
     console.log(checked.parents('tr')
                           .find('td:last')
                           .find('.shareIcon').attr('link'));
 });

Updated JSFiddle 更新了JSFiddle

Try this script: 试试这个脚本:

 $("input[name='check']").on('change', function () {
     var checked = $("input[name='check']:checked");
     $.each(checked, function () {
         if (checked.length >= 1) {
             $.each(checked.parent().siblings().last().children().children(), function() {
                 console.log($(this).data("link"));
             });
         }
     })
 });

And this HTML: 和这个HTML:

<table>
    <tr>
        <td>
            <input type="checkbox" name="check" value="happy">
        </td>
        <td style="width: 310px; line-height: 14px;" class="fileName <?php echo $myClass; ?>">
            <div style="width: 310px; overflow: hidden;"><a href="https://abcd.com/areeb" title="areeb" target="_blank">some_file.php</a>

            </div>
        </td>
        <td style="width: 150px;">212KB</td>
        <td style="width: 150px;">some_file.php</td>
        <td style="width: 150px;"> <a href="javascript:void(0);"><span id="" data-link="https://abcd.com/areeb" title="<?php echo $myinfo['name'];?>" class="shareIcon"></span></a>
 <a href="javascript:void(0);"><span id="deleteIcon" data-link="<?php echo $myBucket['name'];?>" class="deleteIcon"></span></a>

        </td>
    </tr>
</table>

Fiddle 小提琴


Code Explanation: 代码说明:


JavaScript Code: JavaScript代码:

I have just changed you if condition a little bit so that it can match even one element as well. 我只是改变了您的条件,以便它甚至可以匹配一个元素。 Then the $.each() function is also changed a little bit. 然后, $.each()函数也会有所更改。 I am first targetting the parent, td , of your checkbox then I am getting all it's siblings then I select the last sibling then I pop in into it's children that are a or anchor tags and then I pop into the children of those a s aswell that are the span s and then for each of the span I log it's data-link attribute. 我第一个靶向父, td ,您的复选框,然后我得到的所有它的兄弟姐妹,然后我选择最后一个兄弟,然后我突然在到它的孩子,是a或锚标记,然后我弹出到那些孩子们a小号藏汉那是span ,然后为每个span记录data-link属性。

HTML Code: HTML代码:

I have just changed the name of the link attribute to data-link attribute for easier access through the data function. 我刚刚将link属性的名称更改为data-link属性,以便于通过data函数访问。

var inputs = $('input[name="check"]').on('change', function () {
    inputs.each(function () {
        var i = $(this);

        if (i.is(':checked')) {
            console.log(i.closest('tr').find('.shareIcon').attr('link'));
        }
    });
});

fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM