跳过 jQuery 中 $.each 的最后一个索引
skip last index of $.each in jquery
http://jsfiddle.net/jxzwy4d6/
<tr>
<td>abc</td>
<td>Education</td>
<td>abc customer</td>
<td>123</td>
<td>abc@gmail.com</td>
<td>
<button class="edit_record">Edit</button>
</td>
</tr>
我的简斯
$(function () {
$('body').on('click', '.edit_record', function (e) {
$.each($(this).closest('tr'), function (i, obj) {
//get all td text except last one
});
});
});
我试过了
if(i != 5){console.log($(this).find('td').text());}
但它不起作用,则包含最后一个编辑值。
试试这个
$.each($(this).closest('tr').find('td').not(':last'), function (i, obj) {
//get all td text except last one
console.log($(this).html());
});
另一种方法是针对按钮父级的兄弟姐妹...无需以这种方式对索引大惊小怪
$('body').on('click', '.edit_record', function (e) {
$(this).parent().siblings().each(function(){
console.log( $(this).text() );
});
});
参考兄弟姐妹((文档
您的代码永远不会到达索引 5,因为您遍历了 tr
元素。不是它的td
元素,因为表行仅存在 1 个元素,请使用以下代码:
$.each($(this).closest('tr').find('td'), function(i, obj) {
// i right now having value 0 - 5(this is your last index)
});
更新的演示
您可以使用.parent(), .parent('td') or .closest('td')
获取按钮的父级并查找.siblings
,这将是所有td
,但包含按钮的那个。而且,您可以使用.each()
而不是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.com/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>abc@gmail.com</td>
<td>
<button class="edit_record">Edit</button>
</td>
</tr>
</tbody></table>
小提琴
$(document).on('click', '.edit_record', function() {
var table =$(this).closest('tr').find('td:not(:last)');
table.each(function () {
console.log($(this).text());
});
});
你可以这样做。选择除最后一个 td 之外的所有 td,然后迭代所选 td 并获取其各自的文本
相关文章:
- 多维关联数组的最后一个索引
- JavaScript更新最后一个索引类数据
- 如何避免在将索引作为函数的参数发送时引用for循环中的最后一个索引
- 获取具有值的序列中最后一个输入字段的索引
- 使用 jQuery 创建动态元素 单击事件始终返回 for 循环中的最后一个索引
- JavaScript 获取最后一个重复值索引 JavaScript
- 如何使用 Java 脚本中的最后一个索引删除子字符串
- 角度.js在 ng-repeat 中获取 2d 数组中最后一个元素的索引
- 循环的JQuery卡在最后一个索引处
- 可以使用Array.splice()通过在数组最后一个元素之外的索引处添加元素来创建稀疏数组吗
- 在循环中,I'm附加了一个标记,提醒循环索引计数.为什么它总是提醒循环的最后一个索引
- 仅在最后一个索引处检查foreach中的if条件
- 将jQuery/JavaScript中、的最后一个索引替换为和
- DOJO DataGrid-如何获取第一个可见行和最后一个可见行的索引(编号)
- 使用Angular ng-repeat获取数组的最后一个索引
- 将数组中的所有值向上移动一个索引,并将移位的最后一个元素作为第一个元素
- 跳过 jQuery 中 $.each 的最后一个索引
- 尝试从状态数组从索引进行拼接,但最终从第一个索引拼接到最后一个索引
- 如何在下拉列表中选择最后一个索引值
- 查找最后一个导入语句的索引