###前提・実現したいこと
「one」チェックボックスを一つでもチェックすると、「all」チェックボックスが外れ、「one」のチェックが全て外れると「all」がチェックされる。
上記にリストの表示切り替えが連動する。
追記
動くサンプル
https://jsfiddle.net/uetr0pfw/
###発生している問題・エラーメッセージ
期待する動きは実現できたが、下記のエラーが出てしまいます。
エラーメッセージ
Uncaught TypeError: Cannot read property 'addClass' of undefined
###該当のソースコード
html
1<input type="checkbox" class="all"><label>all</label> 2<input type="checkbox" class="one" data-target=".c1"><label>one 1</label> 3<input type="checkbox" class="one" data-target=".c2"><label>one 2</label> 4<ul id="list"> 5<li class="c1">target-one</li> 6<li class="c2">target-two</li> 7</ul>
javascript
1var checkOne = $('.one'); 2var checkAll = $('.all'); 3 4checkAll.on('click, change', function() { 5 if (checkAll.prop('checked')) { 6 $('li').addClass('is-selected'); 7 $('li').prop('data-target').addClass('is-selected'); 8 } 9 }); 10 11checkOne.on('click', function() { 12 if (checkOne.filter(':checked').length) { 13 checkAll.prop('checked', false).prop('disabled', false).change(); 14 } 15 else { 16 checkAll.prop('checked', true).prop('disabled', true).change(); 17 } 18 });
###補足情報(言語/FW/ツール等のバージョンなど)
jQuery v3.2.1
よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/31 06:06
2017/03/31 06:11
2017/03/31 06:15
2017/04/01 14:02