gpt4 book ai didi

javascript - 计算每列jquery js中选中的复选框

转载 作者:行者123 更新时间:2023-11-28 02:30:53 25 4
gpt4 key购买 nike

我发现这段漂亮的代码几乎完美地完成了我的任务,但我需要计算列数而不是行数。

https://codepen.io/b00stup/pen/dzGYPW

$.each(attendance, function(name, days) {
var studentRow = $('tbody .name-col:contains("' + name + '")').parent('tr'),
dayChecks = $(studentRow).children('.attend-col').children('input');

dayChecks.each(function(i) {
$(this).prop('checked', days[i]);
});
});

有人知道如何更改 js 以计算列数而不是行数吗?

最佳答案

你可以这样做:

$('input[type="checkbox"]').change(function() {                        //Add event listener on checkboxes

var result = []; //Init the result variable
$('.student').each(function() { //Select the row ( class stunden ) and loop
$(this).find('input[type="checkbox"]').each(function(i) { //Find all checkbox on a row and loop
result[i] = result[i] || 0; //Init if index does not exist
result[i] += this.checked; // +1 if checked
})
})

console.log( JSON.stringify( result ) );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1>Udacity Attendance</h1>

<table>
<thead>
<tr>
<th class="name-col">Student Name</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th class="missed-col">Days Missed-col</th>
</tr>
</thead>
<tbody>
<tr class="student">
<td class="name-col">Slappy the Frog</td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="missed-col">0</td>
</tr>
<tr class="student">
<td class="name-col">Lilly the Lizard</td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="missed-col">0</td>
</tr>
<tr class="student">
<td class="name-col">Paulrus the Walrus</td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="missed-col">0</td>
</tr>
<tr class="student">
<td class="name-col">Gregory the Goat</td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="missed-col">0</td>
</tr>
<tr class="student">
<td class="name-col">Adam the Anaconda</td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="attend-col"><input type="checkbox"></td>
<td class="missed-col">0</td>
</tr>
</tbody>
</table>

关于javascript - 计算每列jquery js中选中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50875870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com