2017-02-07 2 views
0

クリックしたチェックボックスのすべての属性を取得できますが、それを目視で確認することはできません。チェックボックスをクリックしたときにチェックされた属性を設定できません

$('body').off('click.checkbox').on('click.checkbox', '[data-checkbox]', function(event) { 

    var id = $(this).closest('[data-target]').attr('id'); 
    var target = $(event.target); 

    if (target.is('input')) { 

     if(target.is(":checked") == true) { 

      console.debug($(this).find('input:checkbox').attr('id')); 
      /* 
       item-checkbox-1 
       item-checkbox-2 
      */ 

      //both below ways unable to check it 
      $(this).find('input:checkbox').attr('checked', 'checked'); 
      $(this).find('input:checkbox').prop('checked', true); 


      //but this checks all checkboxes found which is unintended 
      find('input:checkbox').prop('checked', true); 

      } else { 

       //do smtg 
      } 

     } else { 
      selectedItem.splice($.inArray(id, selectedItem),1); 
     } 
    } 

}); 

これは、チェックボックスのように見える、そしてどのようにそのチェックボックスにチェックすることができませんでしクリック:

enter image description here

HTML:あなたは既にと入力タグを参照している

<div class="item-checkbox" data-checkbox> 
    <div class="checkbox"> 
     <input type="checkbox" id="item-checkbox-2"> 
     <label for="item-checkbox-2"></label> 
    </div> 
</div> 

答えて

0

target。したがって、$(this)を直接使用してチェック/チェックを外すことができます。

ですので、以下が有効です。

$(this).attr('checked', 'checked'); 
$(this).prop('checked', true); 
+0

既に試してみましたが、動作しません – 112233

+0

はフィドルを提供することは可能ですか? –

0

tryをタイプセレクタで置き換えてみてください。下のように:

$(this).find('input[type=checkbox]').attr('checked', 'checked'); 
+0

試しても動作しません – 112233

関連する問題