Подсчёт чекбоксов
Проблема заключается в том, что когда выбираю целую строку, то справа чекбоксы считаются хорошо, а снизу не считаются вообще, если выбирать по отдельности (не выбирая всю строку или колонну сразу), то всё работает хорошо. Также не понимаю как сделать подсчёт вообще всех чекбоксов в правом нижнем углу. Можете помочь исправить ошибку с подсчётом чекбоксов при выделении целого ряда, колонны и подсказать как вывести все отмеченные чекбоксы в правый нижний угол.

<table border="1">
<tr>
<th></th>
<th>Select column 1 <input type="checkbox" id="co_1"></th>
<th>Select column 2 <input type="checkbox" id="co_2"></th>
<th>Select column 3 <input type="checkbox" id="co_3"></th>
<th>Select column 4 <input type="checkbox" id="co_4"></th>
<th>Select column 5 <input type="checkbox" id="co_5"></th>
<th></th>
</tr>
<tr>
<th>Select string 1 <input type="checkbox" id="s_1"></th>
<td>1-1 <input type="checkbox" class="c_1" id="c_1"></td>
<td>1-2 <input type="checkbox" class="c_2"id="c_1"></td>
<td>1-3 <input type="checkbox" class="c_3" id="c_1"></td>
<td>1-4 <input type="checkbox" class="c_4" id="c_1"></td>
<td>1-5 <input type="checkbox" class="c_5" id="c_1"></td>
<td><span class="all" id="out">0</span></td>
</tr>
<tr>
<th>Select string 2 <input type="checkbox" id="s_2"></th>
<td>2-1 <input type="checkbox" class="c_1" id="c_2"></td>
<td>2-2 <input type="checkbox" class="c_2" id="c_2"></td>
<td>2-3 <input type="checkbox" class="c_3" id="c_2"></td>
<td>2-4 <input type="checkbox" class="c_4" id="c_2"></td>
<td>2-5 <input type="checkbox" class="c_5" id="c_2"></td>
<td><span class="all" id="out1">0</span></td>
</tr>
<tr>
<th>Select string 3 <input type="checkbox" id="s_3"></th>
<td>3-1 <input type="checkbox" class="c_1" id="c_3"></td>
<td>3-2 <input type="checkbox" class="c_2" id="c_3"></td>
<td>3-3 <input type="checkbox" class="c_3" id="c_3"></td>
<td>3-4 <input type="checkbox" class="c_4" id="c_3"></td>
<td>3-5 <input type="checkbox" class="c_5" id="c_3"></td>
<td><span class="all" id="out2">0</span></td>
</tr>
<tr>
<th>Select string 4 <input type="checkbox" id="s_4"></th>
<td>4-1 <input type="checkbox" class="c_1" id="c_4"></td>
<td>4-2 <input type="checkbox" class="c_2" id="c_4"></td>
<td>4-3 <input type="checkbox" class="c_3" id="c_4"></td>
<td>4-4 <input type="checkbox" class="c_4" id="c_4"></td>
<td>4-5 <input type="checkbox" class="c_5" id="c_4"></td>
<td><span class="all" id="out3">0</span></td>
</tr>
<tr>
<th>Select string 5 <input type="checkbox" id="s_5"></th>
<td>5-1 <input type="checkbox" class="c_1" id="c_5"></td>
<td>5-2 <input type="checkbox" class="c_2" id="c_5"></td>
<td>5-3 <input type="checkbox" class="c_3" id="c_5"></td>
<td>5-4 <input type="checkbox" class="c_4" id="c_5"></td>
<td>5-5 <input type="checkbox" class="c_5" id="c_5"></td>
<td><span class="all" id="out4">0</span></td>
</tr>
<tr>
<th></th>
<td><span class="all" id="out5">0</span></td>
<td><span class="all" id="out6">0</span></td>
<td><span class="all" id="out7">0</span></td>
<td><span class="all" id="out8">0</span></td>
<td><span class="all" id="out9">0</span></td>
<td><span id="out10">0</span></td>
</tr>
</table>
document.getElementById("s_1").addEventListener("change", function() {
document.querySelectorAll('input[id="c_1"]').forEach(c => c.checked = this.checked);
countCheckboxes();
});
document.querySelectorAll('input[id="c_1"]').forEach(checkbox =>
checkbox.addEventListener('change', countCheckboxes)
)
function countCheckboxes() {
out.textContent = document.querySelectorAll('input[id="c_1"]:checked').length;
}
document.getElementById("s_2").addEventListener("change", function() {
document.querySelectorAll('input[id="c_2"]').forEach(c => c.checked = this.checked);
countCheckboxes1();
});
document.querySelectorAll('input[id="c_2"]').forEach(checkbox =>
checkbox.addEventListener('change', countCheckboxes1)
)
function countCheckboxes1() {
out1.textContent = document.querySelectorAll('input[id="c_2"]:checked').length;
}
document.getElementById("s_3").addEventListener("change", function() {
document.querySelectorAll('input[id="c_3"]').forEach(c => c.checked = this.checked);
countCheckboxes2();
});
document.querySelectorAll('input[id="c_3"]').forEach(checkbox =>
checkbox.addEventListener('change', countCheckboxes2)
)
function countCheckboxes2() {
out2.textContent = document.querySelectorAll('input[id="c_3"]:checked').length;
}
document.getElementById("s_4").addEventListener("change", function() {
document.querySelectorAll('input[id="c_4"]').forEach(c => c.checked = this.checked);
countCheckboxes3();
});
document.querySelectorAll('input[id="c_4"]').forEach(checkbox =>
checkbox.addEventListener('change', countCheckboxes3)
)
function countCheckboxes3() {
out3.textContent = document.querySelectorAll('input[id="c_4"]:checked').length;
}
document.getElementById("s_5").addEventListener("change", function() {
document.querySelectorAll('input[id="c_5"]').forEach(c => c.checked = this.checked);
countCheckboxes4();
});
document.querySelectorAll('input[id="c_5"]').forEach(checkbox =>
checkbox.addEventListener('change', countCheckboxes4)
)
function countCheckboxes4() {
out4.textContent = document.querySelectorAll('input[id="c_5"]:checked').length;
}
document.getElementById("co_1").addEventListener("change", function() {
document.querySelectorAll('input[class="c_1"]').forEach(c => c.checked = this.checked);
countCheckboxes5();
});
document.querySelectorAll('input[class="c_1"]').forEach(checkbox =>
checkbox.addEventListener('change', countCheckboxes5)
)
function countCheckboxes5() {
out5.textContent = document.querySelectorAll('input[class="c_1"]:checked').length;
}
document.getElementById("co_2").addEventListener("change", function() {
document.querySelectorAll('input[class="c_2"]').forEach(c => c.checked = this.checked);
countCheckboxes6();
});
document.querySelectorAll('input[class="c_2"]').forEach(checkbox =>
checkbox.addEventListener('change', countCheckboxes6)
)
function countCheckboxes6() {
out6.textContent = document.querySelectorAll('input[class="c_2"]:checked').length;
}
document.getElementById("co_3").addEventListener("change", function() {
document.querySelectorAll('input[class="c_3"]').forEach(c => c.checked = this.checked);
countCheckboxes7();
});
document.querySelectorAll('input[class="c_3"]').forEach(checkbox =>
checkbox.addEventListener('change', countCheckboxes7)
)
function countCheckboxes7() {
out7.textContent = document.querySelectorAll('input[class="c_3"]:checked').length;
}
document.getElementById("co_4").addEventListener("change", function() {
document.querySelectorAll('input[class="c_4"]').forEach(c => c.checked = this.checked);
countCheckboxes8();
});
document.querySelectorAll('input[class="c_4"]').forEach(checkbox =>
checkbox.addEventListener('change', countCheckboxes8)
)
function countCheckboxes8() {
out8.textContent = document.querySelectorAll('input[class="c_4"]:checked').length;
}
document.getElementById("co_5").addEventListener("change", function() {
document.querySelectorAll('input[class="c_5"]').forEach(c => c.checked = this.checked);
countCheckboxes9();
});
document.querySelectorAll('input[class="c_5"]').forEach(checkbox =>
checkbox.addEventListener('change', countCheckboxes9)
)
function countCheckboxes9() {
out9.textContent = document.querySelectorAll('input[class="c_5"]:checked').length;
}
Ответы (1 шт):
Автор решения: Andrey Semykin
→ Ссылка
Надо вешать обработчик сразу на всю таблицу, и пересчитывать строки и столбцы. Примерно так:
let table = document.querySelector('table');
table.addEventListener("change", function() {
countCheckboxes();
countCheckboxes1();
countCheckboxes2();
countCheckboxes3();
countCheckboxes4();
countCheckboxes5();
countCheckboxes6();
// Вписать все оставшиеся функции...
});