[英]detecting if a checkbox is ticked inside a target div with jQuery
I have the following markup, below the closing span tag is a number list items. 我有以下标记,在结束范围标记下面是一个数字列表项。
<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>
This is my jQuery. 这是我的jQuery。 What I am looking to do is check if the one and only checkbox, with a name of 'block_background' is checked. 我想做的是检查是否选中了一个唯一的复选框,名称为“ block_background”。 There are a number of 'block_background' checkboxes on the page, but there will only ever be one within $(this)
which is the .header_block
div. 页面上有许多'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');
}
});
How can I check if it's checked? 如何检查是否已检查?
You can use is(':checked')
to check if something is selected 您可以使用is(':checked')
来检查是否已选择某项
$(this).find(".background_flag_container input:checkbox[name='block_background']").each(function(){
if ($(this).is(':checked')) {
console.log('is checked');
}
});
Although seeing that the checkbox has the id block_background
which should be unique. 虽然看到复选框具有ID block_background
,但该ID应该是唯一的。 You could just do this: 您可以这样做:
if ($('#block_background').is(':checked')) {
console.log('is checked');
}
$("input#block_background]").is(':checked')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.