简体   繁体   English

将表中相同单击行的所有单元格值加载到数组中,但首先和最后<td>

[英]Loading All Cell Values Of Same Clicked Row in a Table into an Array But First and Last <td>

Can you please take a look at This Demo and let me know how I can load all cell values in selected .adder clicked row BUT First <td> and Last <td> and instead add also the user and today to the array as well?你能看看这个演示,让我知道我如何在选定的.adder单击行中加载所有单元格值,首先<td>和最后<td>并将usertoday也添加到数组中吗?

 var arr = []; var d = new Date(); var month = d.getMonth()+1; var day = d.getDate(); var today = d.getFullYear() + '/' + ((''+month).length<2 ? '0' : '') + month + '/' + ((''+day).length<2 ? '0' : '') + day; var user = "foo"; $(".adder").on("click", function(){ console.log($(this).closest('tr').children('td').text()); });
 @import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'); body{padding:30px;} .adder{cursor:pointer;}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <table class="table table-striped"> <tr> <td>1</td> <td>Jill</td> <td>Smith</td> <td>50</td> <td><span class="glyphicon glyphicon-plus adder" aria-hidden="true"></span></td> </tr> <tr> <td>2</td> <td>Eve</td> <td>Jackson</td> <td>94</td> <td><span class="glyphicon glyphicon-plus adder" aria-hidden="true"></span></td> </tr> </table>

you need to use .each() and loop through td and then you need .push() to create an array你需要使用.each()并循环遍历td然后你需要.push()来创建一个数组

var arr = [];
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var today = d.getFullYear() + '/' + ((''+month).length<2 ? '0' : '') + month + '/' + ((''+day).length<2 ? '0' : '') + day;
var user = "foo";

$(".adder").on("click", function(){
 var $this = $(this);
    var getarray = [];
$this.closest('tr').find('td').not($this.closest('tr').find('td:first')).not($this.closest('tr').find('td:last')).each(function(){
    getarray.push($(this).text());
});
    getarray.push(user);
    getarray.push(today);
    alert(getarray);
});

DEMO演示

Note : In this code I used .not() for first td which equal 1 and last td .. is that what you want??注意:在这段代码中,我使用 .not() 作为第一个 td ,它等于 1 和最后一个 td .. 这就是你想要的吗?

this is another way by using :not instead of .not() in js这是在 js 中使用 :not 而不是 .not() 的另一种方式

$this.closest('tr').find('td:not(td:first, td:last)').each(function(){});

DEMO演示

暂无
暂无

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

相关问题 jQuery如何判断我单击的td是第一行还是最后一行,以及行索引和列索引? - Jquery how to judge the td I clicked is the first row or last row,and row index and column index? html嵌套表隐藏或显示表td,方法是先单击表行td - html nested table hide or show table last td by clicking table row first td 单击行时的可扩展表,最后一个单元格除外 - Expandable table when row is clicked, except for last cell 获取单击列中第一个单元格的内容以及HTML表格中单击行的第一个单元格 - Get contents of the first cell in the clicked column and the first cell of the clicked row in a HTML table JavaScript 单击同一行上的按钮时获取第二个单元格值 - JavaScript Getting second <td> cell value when clicked on a button on the same row 如何将HTML表td值放在同一行的新行中 - How to put HTML table td values in a new line with in same row 更改表格列中的所有单元格值,已在行和单元格中使用2D数组 - Change all cell values in table column, already using 2D array for row and cell 单击TD时更改表的行颜色 - Change Row Color of Table When TD Is Clicked 在html表中,将每个数据行的第一个单元格更改为<th scope="row"> , 代替<td>元素,使用jQuery? - In html table, change first cell of each data row to <th scope="row">, instead of a <td> element, using jQuery? 将DIV绝对定位在表行中最后一个按类别选择的TD单元的右侧 - Absolutely Position DIV to the right of the last selected (by class) TD cell in a table row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM