[英]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>
并将user
和today
也添加到数组中吗?
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);
});
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(){});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.