我有以下标记,结束 span 标记下方是一个数字列表项。
<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
分区。
$(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
应该是唯一的。你可以这样做:
if ($('#block_background').is(':checked')) {
console.log('is checked');
}
关于javascript - 使用 jQuery 检测目标 div 中是否勾选了复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14341917/