回答編集履歴

1

回答者要望に併せた

2020/10/11 05:15

投稿

YakumoSaki
YakumoSaki

スコア2027

test CHANGED
@@ -18,21 +18,15 @@
18
18
 
19
19
  <script>
20
20
 
21
- function countCheckedCheckbox(className, sumElement, logname) {
21
+ function countCheckedCheckbox(elements, sumElement, logname) {
22
22
 
23
- const checks = document.getElementsByClassName(className);
23
+ let count = 0;
24
24
 
25
+ for (let i = 0; i < elements.length; i++){
25
26
 
27
+ if (elements[i].checked){
26
28
 
27
- let countA = 0;
28
-
29
- for (let i = 0; i < checks.length; i++) {
30
-
31
- if (checks[i].checked){
32
-
33
- countA = countA + 1 ;
29
+ count = count + 1;
34
-
35
- // console.log(countA)
36
30
 
37
31
  }
38
32
 
@@ -40,9 +34,9 @@
40
34
 
41
35
 
42
36
 
43
- console.log(`${logname}の合計は${countA}です`);
37
+ console.log(`${logname}の合計は${count}です`);
44
38
 
45
- sumElement.innerHTML = countA;
39
+ sumElement.innerHTML = count;
46
40
 
47
41
  }
48
42
 
@@ -50,35 +44,33 @@
50
44
 
51
45
  function checkbox() {
52
46
 
53
-
54
-
55
- // チェックされたチェックボックスを取得
56
-
57
47
  const checksA = document.getElementsByClassName("checks_one");
58
48
 
59
49
  const checksB = document.getElementsByClassName("checks_two");
60
50
 
61
- const btn = document.getElementById("btn");
51
+ const btn = document.getElementById("btn")
62
52
 
63
53
 
64
54
 
65
55
  btn.addEventListener("click", () => {
66
56
 
57
+
58
+
67
59
  const countSumA = document.getElementById("sum_one")
68
60
 
69
61
  const countSumB = document.getElementById("sum_two")
70
62
 
71
-
72
63
 
73
- countCheckedCheckbox("checks_one", countSumA, "タイプA");
74
64
 
65
+ countCheckedCheckbox(checksA, countSumA, "タイプA");
66
+
75
- countCheckedCheckbox("checks_two", countSumB, "タイプB");
67
+ countCheckedCheckbox(checksB, countSumB, "タイプB");
76
68
 
77
69
  })
78
70
 
79
71
  }
80
72
 
81
- window.addEventListener("load",checkbox);
73
+ window.addEventListener("load",checkbox);
82
74
 
83
75
  </script>
84
76