回答編集履歴

2

調整

2018/04/06 08:11

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -126,7 +126,7 @@
126
126
 
127
127
  var c=$(this).closest('ul').find('input[name="chk_fluits"]');
128
128
 
129
- if(c.filter(':checked').length==2) p.prop('checked',true);
129
+ if(c.filter(':not(:checked)').length==0) p.prop('checked',true);/*改良*/
130
130
 
131
131
  if(c.filter(':checked').length==0) p.prop('checked',false);
132
132
 

1

追記

2018/04/06 08:10

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -61,3 +61,133 @@
61
61
  </ul>
62
62
 
63
63
  ```
64
+
65
+
66
+
67
+ # 追記
68
+
69
+
70
+
71
+ なるほど仕様がいろいろあるのですね
72
+
73
+ - ピンクの領域をクリックすると、チェックが一つでもあれば消え、何もなければ全チェック
74
+
75
+ - 親をチェックすると子が全チェック、親のチェックを外すと子のチェックが全部はずれる
76
+
77
+ - 子のチェックを両方すると親がチェックされ、このチェックを両方はずすと親のチェックが外れる
78
+
79
+ - このチェックが片方の場合は親は反応しない
80
+
81
+
82
+
83
+ 上記の仕様をを入れ込むとこんな感じでしょうか?
84
+
85
+ いずれにしろ、親に対するラベルは辞めたほうがいいです。
86
+
87
+ ```CSS
88
+
89
+ .sitsumon {
90
+
91
+ background: aqua;
92
+
93
+ }
94
+
95
+ .c-checklist li {
96
+
97
+ background: pink;
98
+
99
+ margin: 10px;
100
+
101
+ list-style: none;
102
+
103
+ padding: 5px;
104
+
105
+ }
106
+
107
+ .c-checklist li ul li label {
108
+
109
+ background: #000;
110
+
111
+ color: #FFF;
112
+
113
+ padding: 1px 10px 1px 10px;
114
+
115
+ }
116
+
117
+ ```
118
+
119
+ ```javascript
120
+
121
+ $(function(){
122
+
123
+ $('input[name="chk_fluits"]').on('change',function(){
124
+
125
+ var p=$(this).closest('ul').closest('li').find('input[name="chk_fluits2"]');
126
+
127
+ var c=$(this).closest('ul').find('input[name="chk_fluits"]');
128
+
129
+ if(c.filter(':checked').length==2) p.prop('checked',true);
130
+
131
+ if(c.filter(':checked').length==0) p.prop('checked',false);
132
+
133
+ }).trigger('change');
134
+
135
+ $('input[name="chk_fluits2"]').on('change',function(){
136
+
137
+ $(this).siblings().find('input[name="chk_fluits"]').prop('checked',$(this).prop('checked'));
138
+
139
+ }).trigger('change');
140
+
141
+ $('.c-checklist input[type=checkbox],.check_label').on('click',function(e){
142
+
143
+ e.stopPropagation();
144
+
145
+ });
146
+
147
+ $('.c-checklist>li').on('click',function(e){
148
+
149
+ $(this).find('[type=checkbox]').prop('checked',$(this).find('[type=checkbox]:checked').length==0?true:false);
150
+
151
+ });
152
+
153
+ });
154
+
155
+ ```
156
+
157
+ ```HTML
158
+
159
+ <ul class="c-checklist">
160
+
161
+ <li><input id="c-apple" type="checkbox" name="chk_fluits2">
162
+
163
+ <p>apple</p>
164
+
165
+ <ul>
166
+
167
+ <li><input id="c-apple-1" class="check-c check-1" type="checkbox" name="chk_fluits" value="c-apple-1" target="c-apple-1"><label for="c-apple-1" class="check_label">1</label></li>
168
+
169
+ <li><input id="c-apple-2" class="check-c check-2" type="checkbox" name="chk_fluits" value="c-apple-2" target="c-apple-2"><label for="c-apple-2" class="check_label">2</label></li>
170
+
171
+ </ul>
172
+
173
+ </li>
174
+
175
+ <li><input id="c-grape" type="checkbox" name="chk_fluits2">
176
+
177
+ <p>grape</p>
178
+
179
+ <ul>
180
+
181
+ <li><input id="c-grape-1" class="check-c check-1" type="checkbox" name="chk_fluits" value="c-grape-1" target="c-grape-1"><label for="c-grape-1" class="check_label">1</label></li>
182
+
183
+ <li><input id="c-grape-2" class="check-c check-2" type="checkbox" name="chk_fluits" value="c-grape-2" target="c-grape-2"><label for="c-grape-2" class="check_label">2</label></li>
184
+
185
+ </ul>
186
+
187
+ </label>
188
+
189
+ </li>
190
+
191
+ </ul>
192
+
193
+ ```