回答編集履歴

1

変更

2017/07/25 03:20

投稿

退会済みユーザー
test CHANGED
@@ -1,3 +1,95 @@
1
+ ```
2
+
3
+ showResult(); // 初期全てを非表示にするため
4
+
5
+
6
+
7
+ $(".test input[type=checkbox]").click(function () {
8
+
9
+ showResult();
10
+
11
+ });
12
+
13
+
14
+
15
+ function showResult(){
16
+
17
+ var $section = $('section');
18
+
19
+ var $checked_a = $('[id^="a"]:checked');
20
+
21
+ var $checked_b = $('[id^="b"]:checked');
22
+
23
+
24
+
25
+ var _filter = function ($checked) {
26
+
27
+ return function () {
28
+
29
+ var $this = $(this);
30
+
31
+ return Array.prototype.some.call($checked, function (checkbox) {
32
+
33
+ return $this.hasClass(checkbox.getAttribute('id'));
34
+
35
+ });
36
+
37
+ }
38
+
39
+ };
40
+
41
+
42
+
43
+ // 一旦すべてのsectionを非表示にする
44
+
45
+ $section.hide();
46
+
47
+
48
+
49
+ // a、bの全てが未選択の場合、処理中断
50
+
51
+ if (($checked_a.length === 0) && ($checked_b.length === 0)) {
52
+
53
+ return;
54
+
55
+ }
56
+
57
+
58
+
59
+ // チェックボックスのid属性の値とsectionのクラス属性の値でフィルタリング
60
+
61
+ if ($checked_a.length > 0) {
62
+
63
+ $section = $section.filter(_filter($checked_a));
64
+
65
+ }
66
+
67
+
68
+
69
+ if ($checked_b.length > 0) {
70
+
71
+ $section = $section.filter(_filter($checked_b));
72
+
73
+ }
74
+
75
+
76
+
77
+ $section.show();
78
+
79
+ }
80
+
81
+ ```
82
+
83
+ [https://jsfiddle.net/takmatz/psbz54ad/](https://jsfiddle.net/takmatz/psbz54ad/)
84
+
85
+
86
+
87
+ ---
88
+
89
+
90
+
91
+ 修正前
92
+
1
93
  ``` javascript
2
94
 
3
95
  showResult(); // 初期全てを非表示にするため