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.
5 Answers
Reset to default 3Try 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 td
s 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
版权声明:本文标题:javascript - skip last index of $.each in jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742122869a2421804.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论