LoginSignup
1
3

More than 5 years have passed since last update.

Jqueryで div 全体をクリックしたときにチェックボックスがクリックされたようにする。

Posted at

rails で

タグ全体をチェックした時にチェックボックスを有効にしたい時。ハマったので。

<div class="col-sm-4 checkbox-row">
    <%= check_box_tag "medicine_request[#{i}]", medicine, cold_add_question_boolean(@medicine_request,medicine) %>
    <%= label_tag "medicine_request[#{i}]", medicine %>
</div>



//check_boxの行全体クリック処理
$('.checkbox-row').on('click', function(e) {
//td要素からチェックボックスを探す
    var c = $(this).children('input[type="checkbox"]');
    if (e.target != c[0]) {
      if(c.prop('checked')){
          c.prop('checked','').change();
      }else{
          c.prop('checked', 'checked').change();
      }
    }
});

下記の実装をしていたら、チェックボックス自体と、coflictを起こしてしまった。

//checkboxで、列(div)ごと押すとチェックされるように実装
$('.checkbox-default').on('click', function(){
    if ($(this).attr('checked',false)){
       $(this).find('input[type=checkbox]').attr('checked', true); 
    }
});

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3