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

質問編集履歴

3

htmlとjavascriptを改めました。

2017/04/30 12:42

投稿

mekadoq
mekadoq

スコア10

title CHANGED
File without changes
body CHANGED
@@ -11,7 +11,7 @@
11
11
  ```html
12
12
  <div id="search">
13
13
  <ul id="attribute">
14
- <li><label class="selected"><input type="checkbox" id="all" class="sort" checked="checked">すべて</label></li>
14
+ <li><label class="selected"><input type="checkbox" id="all_attribute" class="sort" checked="checked">すべて</label></li>
15
15
  <li><label><input type="checkbox" id="attribute01" class="sort">attribute01</label></li>
16
16
  <li><label><input type="checkbox" id="attribute02" class="sort">attribute02</label></li>
17
17
  <li><label><input type="checkbox" id="attribute03" class="sort">attribute03</label></li>
@@ -20,7 +20,7 @@
20
20
  </ul>
21
21
 
22
22
  <ul id="type">
23
- <li><label class="selected"><input type="checkbox" id="all" class="sort" checked="checked">すべて</label></li>
23
+ <li><label class="selected"><input type="checkbox" id="all_type" class="sort" checked="checked">すべて</label></li>
24
24
  <li><label><input type="checkbox" id="type01" class="sort">type01</label></li>
25
25
  <li><label><input type="checkbox" id="type02" class="sort">type02</label></li>
26
26
  <li><label><input type="checkbox" id="type03" class="sort">type03</label></li>
@@ -48,41 +48,76 @@
48
48
  ```
49
49
  ```javascript
50
50
  $( function() {
51
- $( '#search li label input:not(#all)' ).click( function() {
51
+ $( '#search li label input:not(#all_attribute)' ).click( function() {
52
- var inputkey = [ 'attribute', 'type', 'option' ]
52
+ var inputkey = [ 'attribute', 'type' ]
53
- , i, l = inputkey.length
53
+ , i, l = inputkey.length
54
- , $res, $tmp, res = []
54
+ , $res, $tmp, res = []
55
- , $all = $( '#all' );
55
+ , $all = $( '#all_attribute' );
56
- ;
56
+ ;
57
- $tmp = $( '#list > li' ).removeClass( 'active' );
57
+ $tmp = $( '#list > li' ).removeClass( 'active' );
58
- for ( i = 0; i < l; i++ ) {
58
+ for ( i = 0; i < l; i++ ) {
59
- res = []
59
+ res = []
60
- $( '#search input[id^="' + inputkey[ i ] + '"]:checked' ).each( function() {
60
+ $( '#search input[id^="' + inputkey[ i ] + '"]:checked' ).each( function() {
61
- res.push( $( this )[ 0 ].id );
61
+ res.push( $( this )[ 0 ].id );
62
- } );
62
+ } );
63
- if ( !res.length ) { continue; }
63
+ if ( !res.length ) { continue; }
64
- $tmp = $( '#list > li.' + res.join( ', #list > li.' ) );
64
+ $tmp = $( '#list > li.' + res.join( ', #list > li.' ) );
65
- if ( !$res || !$res.length ) {
65
+ if ( !$res || !$res.length ) {
66
- $res = $tmp;
66
+ $res = $tmp;
67
- } else if ( $tmp.length ) {
67
+ } else if ( $tmp.length ) {
68
- $res = $res.filter( '.' + res.join( ', .' ) );
68
+ $res = $res.filter( '.' + res.join( ', .' ) );
69
- }
69
+ }
70
- }
70
+ }
71
- if ( $res && $res.length ) {
71
+ if ( $res && $res.length ) {
72
- $res.addClass( 'active' );
72
+ $res.addClass( 'active' );
73
- $all.prop( 'checked', false );
73
+ $all.prop( 'checked', false );
74
- } else {
74
+ } else {
75
- // No Selected
75
+ // No Selected
76
- res = false;
76
+ res = false;
77
- $all.prop( 'checked', true );
77
+ $all.prop( 'checked', true );
78
- }
78
+ }
79
- } );
79
+ } );
80
- $( '#all' ).click( function() {
80
+ $( '#all_attribute' ).click( function() {
81
- if ( $( this ).prop( 'checked') === true ) {
81
+ if ( $( this ).prop( 'checked') === true ) {
82
- $( '#list > li' ).removeClass( 'active' );
82
+ $( '#list > li' ).removeClass( 'active' );
83
- $( '#search input:checked:not(#all)' ).prop( 'checked', false );
83
+ $( '#search input:checked:not(#all_attribute)' ).prop( 'checked', false );
84
- }
84
+ }
85
- } );
85
+ } );
86
+ $( '#search li label input:not(#all_type)' ).click( function() {
87
+ var inputkey = [ 'attribute', 'type' ]
88
+ , i, l = inputkey.length
89
+ , $res, $tmp, res = []
90
+ , $all = $( '#all_attribute' );
91
+ ;
92
+ $tmp = $( '#list > li' ).removeClass( 'active' );
93
+ for ( i = 0; i < l; i++ ) {
94
+ res = []
95
+ $( '#search input[id^="' + inputkey[ i ] + '"]:checked' ).each( function() {
96
+ res.push( $( this )[ 0 ].id );
97
+ } );
98
+ if ( !res.length ) { continue; }
99
+ $tmp = $( '#list > li.' + res.join( ', #list > li.' ) );
100
+ if ( !$res || !$res.length ) {
101
+ $res = $tmp;
102
+ } else if ( $tmp.length ) {
103
+ $res = $res.filter( '.' + res.join( ', .' ) );
104
+ }
105
+ }
106
+ if ( $res && $res.length ) {
107
+ $res.addClass( 'active' );
108
+ $all.prop( 'checked', false );
109
+ } else {
110
+ // No Selected
111
+ res = false;
112
+ $all.prop( 'checked', true );
113
+ }
114
+ } );
115
+ $( '#all_type' ).click( function() {
116
+ if ( $( this ).prop( 'checked') === true ) {
117
+ $( '#list > li' ).removeClass( 'active' );
118
+ $( '#search input:checked:not(#all_type)' ).prop( 'checked', false );
119
+ }
120
+ } );
86
121
  } );
87
122
  ```
88
123
  ```css

2

jsfiddleで試したことを追記しました。

2017/04/30 12:42

投稿

mekadoq
mekadoq

スコア10

title CHANGED
File without changes
body CHANGED
@@ -96,4 +96,13 @@
96
96
  #search > ul {
97
97
  float: left;
98
98
  }
99
- ```
99
+ ```
100
+
101
+
102
+
103
+ 以下、追記いたします。
104
+
105
+ [https://jsfiddle.net/mekadoq/m5z9hezb/2/](https://jsfiddle.net/mekadoq/m5z9hezb/2/)
106
+
107
+ 上記URLのように、2つある「すべて」のidをそれぞれ「all_attribute」「all_type」として、javascriptもそれぞれに対応させようとしたところ、各選択肢にチェックをいれた際に「すべて」のチェックが外れません。
108
+ また、「.addClass( 'active' );」「.removeClass( 'active' );」が実行されなくなってしまいました。

1

jsfiddleリンクを追加

2017/04/30 12:18

投稿

mekadoq
mekadoq

スコア10

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  こちらのソースを元に作成しているのですが、下記の条件に合わせてアレンジしようとしています。
2
2
  「attribute」「type」のどちらにもそれぞれに対応する「すべて」をつけた場合、jqueryがうまくできませんでした。
3
- https://jsfiddle.net/jwqyan5j/2/
3
+ [https://jsfiddle.net/jwqyan5j/2/](https://jsfiddle.net/jwqyan5j/2/)
4
4
 
5
5
  ・「attribute」「type」の2つの条件で絞り込むAND検索
6
6
  ・デフォルトで「attribute」「type」どちらも「すべて」にチェックがはいっている。