![](/img/trans.png)
[英]Jquery how to judge the td I clicked is the first row or last row,and row index and column index?
[英]Loading All Cell Values Of Same Clicked Row in a Table into an Array But First and Last <td>
你能看看這個演示,讓我知道我如何在選定的.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>
你需要使用.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);
});
注意:在這段代碼中,我使用 .not() 作為第一個 td ,它等於 1 和最后一個 td .. 這就是你想要的嗎?
這是在 js 中使用 :not 而不是 .not() 的另一種方式
$this.closest('tr').find('td:not(td:first, td:last)').each(function(){});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.