簡體   English   中英

使用jQuery檢測復選框是否在目標div中被勾選

[英]detecting if a checkbox is ticked inside a target div with 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>

這是我的jQuery。 我想做的是檢查是否選中了一個唯一的復選框,名稱為“ block_background”。 頁面上有許多'block_background'復選框,但是$(this)只有一個 ,它是.header_block div。

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

如何檢查是否已檢查?

您可以使用is(':checked')來檢查是否已選擇某項

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

雖然看到復選框具有ID block_background ,但該ID應該是唯一的。 您可以這樣做:

if ($('#block_background').is(':checked')) {
    console.log('is checked');
}
$("input#block_background]").is(':checked')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM