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);
}
});