admin管理员组

文章数量:1323723

/

<tr>
    <td>abc</td>
    <td>Education</td>
    <td>abc customer</td>
    <td>123</td>
    <td>[email protected]</td>
    <td>
        <button class="edit_record">Edit</button>
    </td>
</tr>

My JS

$(function () {
    $('body').on('click', '.edit_record', function (e) {
        $.each($(this).closest('tr'), function (i, obj) {
            //get all td text except last one


        });

    });

});

I tried

if(i != 5){console.log($(this).find('td').text());} but it doesn't work, the last Edit value is included.

http://jsfiddle/jxzwy4d6/

<tr>
    <td>abc</td>
    <td>Education</td>
    <td>abc customer</td>
    <td>123</td>
    <td>[email protected]</td>
    <td>
        <button class="edit_record">Edit</button>
    </td>
</tr>

My JS

$(function () {
    $('body').on('click', '.edit_record', function (e) {
        $.each($(this).closest('tr'), function (i, obj) {
            //get all td text except last one


        });

    });

});

I tried

if(i != 5){console.log($(this).find('td').text());} but it doesn't work, the last Edit value is included.

Share Improve this question asked Sep 10, 2015 at 1:57 Siva Natalie Siva Natalie 1819 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

Try this

$.each($(this).closest('tr').find('td').not(':last'), function (i, obj) {
    //get all td text except last one
     console.log($(this).html());
});

Another approach is to target the siblings of the button's parent ... no need to fuss with index this way

$('body').on('click', '.edit_record', function (e) {
    $(this).parent().siblings().each(function(){
         console.log( $(this).text() );
     });
});

reference siblings() docs

Your code will never reach index 5 because, you loop over the tr element. Not its td element, as table rows only existed 1 element only, use below code :

$.each($(this).closest('tr').find('td'), function(i, obj) {
 // i right now having value 0 - 5(this is your last index)
});

Updated DEMO

You can get the parent, using .parent(), .parent('td') or .closest('td'), of the button and the find the .siblings, which will be all tds but the one containing the button. And, you can use .each() instead of jQuery.each().:

  $(this).closest('td').siblings().each(function(i, td) {
      //The last td is excluded here as only the siblings are iterated.
  });

$(function() {
  $(document).on('click', '.edit_record', function() {
      $(this).closest('td').siblings().each(function(i, td) {
          console.log( td );
      });
  });
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table><tbody>
  <tr>
    <td>abc</td>
    <td>Education</td>
    <td>abc customer</td>
    <td>123</td>
    <td>[email protected]</td>
    <td>
        <button class="edit_record">Edit</button>
    </td>
</tr>
  </tbody></table>

FIDDLE

$(document).on('click', '.edit_record', function() {   
    var table =$(this).closest('tr').find('td:not(:last)');
    table.each(function () {
        console.log($(this).text());
    });
});

You can do it this way.Select all td except the last one then iterate on the selected td and get their respective text

本文标签: javascriptskip last index of each in jqueryStack Overflow