检测是否使用jQuery在目标div内勾选了一个复选框

crm*_*cco 1 html javascript forms checkbox jquery

我有以下标记,在结束范围标记下方是一个数字列表项.

<div class="header_block">
   <span class="background_flag_container">
      <input id="block_background" type="checkbox" name="block_background">
      <span class="background_flag_text">Has Banner Background</span>
   </span>
   ...
</div>
Run Code Online (Sandbox Code Playgroud)

这是我的jQuery.我要做的是检查是否选中了名为"block_background"的唯一复选框.有一些网页上"block_background"复选框,但只有将永远是一个$(this)这是.header_blockDIV.

$(this).find(".background_flag_container input:checkbox[name='block_background']").each(function(){
   if ($(this).checked) {
      console.log('is checked');
   }
});
Run Code Online (Sandbox Code Playgroud)

如何检查是否已检查?

Pez*_*kow 5

您可以 is(':checked')用来检查是否选择了某些内容

   $(this).find(".background_flag_container input:checkbox[name='block_background']").each(function(){
       if ($(this).is(':checked')) {
          console.log('is checked');
       }
    });
Run Code Online (Sandbox Code Playgroud)

虽然看到复选框的id block_background应该是唯一的.你可以这样做:

if ($('#block_background').is(':checked')) {
    console.log('is checked');
}
Run Code Online (Sandbox Code Playgroud)