質問するログイン新規登録

回答編集履歴

1

変更

2017/07/25 03:20

投稿

退会済みユーザー
answer CHANGED
@@ -1,3 +1,49 @@
1
+ ```
2
+ showResult(); // 初期全てを非表示にするため
3
+
4
+ $(".test input[type=checkbox]").click(function () {
5
+ showResult();
6
+ });
7
+
8
+ function showResult(){
9
+ var $section = $('section');
10
+ var $checked_a = $('[id^="a"]:checked');
11
+ var $checked_b = $('[id^="b"]:checked');
12
+
13
+ var _filter = function ($checked) {
14
+ return function () {
15
+ var $this = $(this);
16
+ return Array.prototype.some.call($checked, function (checkbox) {
17
+ return $this.hasClass(checkbox.getAttribute('id'));
18
+ });
19
+ }
20
+ };
21
+
22
+ // 一旦すべてのsectionを非表示にする
23
+ $section.hide();
24
+
25
+ // a、bの全てが未選択の場合、処理中断
26
+ if (($checked_a.length === 0) && ($checked_b.length === 0)) {
27
+ return;
28
+ }
29
+
30
+ // チェックボックスのid属性の値とsectionのクラス属性の値でフィルタリング
31
+ if ($checked_a.length > 0) {
32
+ $section = $section.filter(_filter($checked_a));
33
+ }
34
+
35
+ if ($checked_b.length > 0) {
36
+ $section = $section.filter(_filter($checked_b));
37
+ }
38
+
39
+ $section.show();
40
+ }
41
+ ```
42
+ [https://jsfiddle.net/takmatz/psbz54ad/](https://jsfiddle.net/takmatz/psbz54ad/)
43
+
44
+ ---
45
+
46
+ 修正前
1
47
  ``` javascript
2
48
  showResult(); // 初期全てを非表示にするため
3
49