Jquery选择div元素内的复选框
我在div元素中有一个复选框。我想在每次单击按钮时将复选框标记为选中。。div元素标记如下所示Jquery选择div元素内的复选框,jquery,Jquery,我在div元素中有一个复选框。我想在每次单击按钮时将复选框标记为选中。。div元素标记如下所示 <div class ="test123" style ="width:90px;"> <input id = "chk1" type = "checkbox" value="on"/> </div> 请建议 导航。您可以通过 $('#btn').click(function(){ $('#chk1').attr("checked","true"); }) &
<div class ="test123" style ="width:90px;">
<input id = "chk1" type = "checkbox" value="on"/>
</div>
请建议
导航。您可以通过
$('#btn').click(function(){
$('#chk1').attr("checked","true");
})
<input type="button" id="btn" />
$('#btn')。单击(函数(){
$('#chk1').attr(“选中”、“真”);
})
这两种方法都很适合你
HTML
而不是上面的一个。。试用
$("#button").click(function() {
$(".test123").find("input:checkbox").each(function() {
if($(this).attr('checked')) {
} else {
$(this).attr('checked','checked');
}
});
});
解决方案的最佳方法是使用标签,而不是像这样的div
<label class ="test123" style ="width:90px;">
<input id="chk1" type="checkbox" value="on"/>
</label>
因此,您将不需要javascript代码 但这意味着,我需要点击div,对吗?我想要的是,点击按钮并选中div中的复选框。但这意味着,我需要点击div,对吗?我想要的是,单击按钮并选中div中的复选框。。
<div class ="test123" style ="width:90px;">
<input id = "chk1" type = "checkbox" value="on"/>
</div>
<input type="button" class="btn" />
$(".test123 ").find("input:checked").each(function() {
$("#button").click(function() {
$(".test123").find("input:checkbox").each(function() {
if($(this).attr('checked')) {
} else {
$(this).attr('checked','checked');
}
});
});
$('#btn').click(function(){
$('.test123').find('input[type=checkbox]').attr('checked','true');
});
<label class ="test123" style ="width:90px;">
<input id="chk1" type="checkbox" value="on"/>
</label>