前提・実現したいこと
チェックボックスの全選択、全解除が出来るチェックボックスを実装したい。
発生している問題・エラーメッセージ
チェックボックスの全選択、全解除が反応しない。
該当のソースコード
rails4
1<%= form_tag({ controller: :goods, action: :destroy_all },method: 'delete') do %> 2 <table class="table"> 3 <thead> 4 <tr> 5 <th>商品画像</th> 6 <th>商品ID</th> 7 <th>商品名</th> 8 <th>価格</th> 9 <th>登録日</th> 10 <th>メーカー</th> 11 <th>カテゴリー</th> 12 <th colspan="3"></th> 13 <th><input type="checkbox" id="checkALL" name="category_all"></th> 14 </tr> 15 </thead> 16 <tbody> 17 <% @goods.each do |good| %> 18 <tr> 19 <td text-align:center;><img height="80" src="<%= good.image_url %>"/></td> 20 <td><%= good.goods_id %></td> 21 <td><%= good.name %></td> 22 <td>¥<%= (good.price).to_s(:delimited) %></td> 23 <td><%=h ((good.date).to_date).strftime('%Y年%m月%d日') %></td> 24 <td><%= good.maker %></td> 25 <td><%= good.category %></td> 26 <td><%= link_to '詳細', good, class: 'btn' %></td> 27 <td><%= link_to '編集', edit_good_path(good), class: 'btn' %></td> 28 <td><%= link_to '削除', good, method: :delete, data: { confirm: '本当に削除してもよろしいですか?' }, class: 'btn' %></td> 29 <td> 30 <%= check_box_tag 'goods[]', good.id %> 31 </td> 32 </tr> 33 <% end %> 34 <%= submit_tag "一括削除" %> 35 </div> 36 </tbody> 37 </div> 38 </table> 39<% end %>
javascript
1$(function(){ 2 3 var checkAll = '#checkAll'; //「すべて」のチェックボックスのidを指定 4 var checkBox = 'input[name="goods[]"]'; //チェックボックスのnameを指定 5 6 $( checkAll ).on('click', function() { 7 $( checkBox ).prop('checked', $(this).is(':checked') ); 8 }); 9 10 $( checkBox ).on( 'click', function() { 11 var boxCount = $( checkBox ).length; //全チェックボックスの数を取得 12 var checked = $( checkBox + ':checked' ).length; //チェックされているチェックボックスの数を取得 13 if( checked === boxCount ) { 14 $( checkAll ).prop( 'checked', true ); 15 } else { 16 $( checkAll ).prop( 'checked', false ); 17 } 18 }); 19 20});
試したこと
チェックボックスの名前を変更したり、ID指定に変更したり、など情報を変えてみましたが、どれもだめでした。
補足情報(FW/ツールのバージョンなど)
特になし。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/29 06:41
2019/01/29 06:45
2019/01/29 07:06