[英]How to get and store the values of a specific row in a table via JavaScript / jQuery
如何將表格中每個選定行的值存儲在 JavaScript / jQuery 的 arrays 數組中。
當單擊“顯示選定”按鈕時,要收聽的事件將是(我認為)。
例如在 JS 中,我們會有一個這樣的數組:
result = []
result[0] = row1
result[0][1] = Gene value for row1 (Gene1)
result[0][2] = Variant value for row1 (Variant1)
result[0][7] = #5 value1 for row1
result[1] = row2
result[1][1] = Gene value for row2 (Gene2)
etc
...
你可以這樣做:
$("#show-selected").click(function () {
var results =[];
$(".variants-table-tbody tr").each(function (index,item) {
if($(item).find('[name="select-item"]').prop("checked")){
results.push([
[$(item).find("td").eq(1).text()],
[$(item).find("td").eq(2).text()],
[$(item).find("td").eq(3).text()],
]);
}
});
console.log(results);
});
這將以您描述的格式在選中的行上創建數據。 見https://jsfiddle.net/y4deut3s/
像這樣:
$("#show-selected").click(function () {
var tr = $("tr");
var array1 = [];
tr.each(function(index1, trCurrent) {
var array2 = [];
var trChildrens = $(trCurrent).children();
if(($(trChildrens[0]).children()[0]).checked){
trChildrens.each(function(index2, trChildrensCurrent) {
var innerHtml = $(trChildrensCurrent).html();
array2.push(innerHtml);
});
array1.push(array2);
}
});
console.log(array1);
});
測試:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.