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

回答編集履歴

2

調整

2018/04/06 08:11

投稿

yambejp
yambejp

スコア117946

answer CHANGED
@@ -62,7 +62,7 @@
62
62
  $('input[name="chk_fluits"]').on('change',function(){
63
63
  var p=$(this).closest('ul').closest('li').find('input[name="chk_fluits2"]');
64
64
  var c=$(this).closest('ul').find('input[name="chk_fluits"]');
65
- if(c.filter(':checked').length==2) p.prop('checked',true);
65
+ if(c.filter(':not(:checked)').length==0) p.prop('checked',true);/*改良*/
66
66
  if(c.filter(':checked').length==0) p.prop('checked',false);
67
67
  }).trigger('change');
68
68
  $('input[name="chk_fluits2"]').on('change',function(){

1

追記

2018/04/06 08:10

投稿

yambejp
yambejp

スコア117946

answer CHANGED
@@ -29,4 +29,69 @@
29
29
  </label>
30
30
  </li>
31
31
  </ul>
32
+ ```
33
+
34
+ # 追記
35
+
36
+ なるほど仕様がいろいろあるのですね
37
+ - ピンクの領域をクリックすると、チェックが一つでもあれば消え、何もなければ全チェック
38
+ - 親をチェックすると子が全チェック、親のチェックを外すと子のチェックが全部はずれる
39
+ - 子のチェックを両方すると親がチェックされ、このチェックを両方はずすと親のチェックが外れる
40
+ - このチェックが片方の場合は親は反応しない
41
+
42
+ 上記の仕様をを入れ込むとこんな感じでしょうか?
43
+ いずれにしろ、親に対するラベルは辞めたほうがいいです。
44
+ ```CSS
45
+ .sitsumon {
46
+ background: aqua;
47
+ }
48
+ .c-checklist li {
49
+ background: pink;
50
+ margin: 10px;
51
+ list-style: none;
52
+ padding: 5px;
53
+ }
54
+ .c-checklist li ul li label {
55
+ background: #000;
56
+ color: #FFF;
57
+ padding: 1px 10px 1px 10px;
58
+ }
59
+ ```
60
+ ```javascript
61
+ $(function(){
62
+ $('input[name="chk_fluits"]').on('change',function(){
63
+ var p=$(this).closest('ul').closest('li').find('input[name="chk_fluits2"]');
64
+ var c=$(this).closest('ul').find('input[name="chk_fluits"]');
65
+ if(c.filter(':checked').length==2) p.prop('checked',true);
66
+ if(c.filter(':checked').length==0) p.prop('checked',false);
67
+ }).trigger('change');
68
+ $('input[name="chk_fluits2"]').on('change',function(){
69
+ $(this).siblings().find('input[name="chk_fluits"]').prop('checked',$(this).prop('checked'));
70
+ }).trigger('change');
71
+ $('.c-checklist input[type=checkbox],.check_label').on('click',function(e){
72
+ e.stopPropagation();
73
+ });
74
+ $('.c-checklist>li').on('click',function(e){
75
+ $(this).find('[type=checkbox]').prop('checked',$(this).find('[type=checkbox]:checked').length==0?true:false);
76
+ });
77
+ });
78
+ ```
79
+ ```HTML
80
+ <ul class="c-checklist">
81
+ <li><input id="c-apple" type="checkbox" name="chk_fluits2">
82
+ <p>apple</p>
83
+ <ul>
84
+ <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>
85
+ <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>
86
+ </ul>
87
+ </li>
88
+ <li><input id="c-grape" type="checkbox" name="chk_fluits2">
89
+ <p>grape</p>
90
+ <ul>
91
+ <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>
92
+ <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>
93
+ </ul>
94
+ </label>
95
+ </li>
96
+ </ul>
32
97
  ```