簡體   English   中英

將表中相同單擊行的所有單元格值加載到數組中,但首先和最后<td>

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

你能看看這個演示,讓我知道我如何在選定的.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>

你需要使用.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM