我有一个动态的形式,下面是结构
>
is_required
类添加到父类
checkboxes_required
将is_required下的所有复选框换行如下
<div class="form-group is_required">
<label>your interestes</label><br>
<div class="checkboxes_required">
<input type="checkbox" name="your_interestes[]" value="Games">Games
<input type="checkbox" name="your_interestes[]" value="study">study
<input type="checkbox" name="your_interestes[]" value="programming">programming
<input type="checkbox" name="your_interestes[]" value="work">work
</div>
</div>
我可以在is_required上循环,然后找到checkboxes_required,如下所示,但不知道如何传递这个as as选择器以进一步循环复选框
$(".is_required").each(function() {
var element = $(this);
$(element).children().each(function () {
if($(this).hasClass("checkboxes_required")){
var checkboxElement = $(this);//here i have access to the div holding checkboxes
}
}
下面的代码在复选框上循环到,但我不确定如何将选择器传递给
$($("selector here ", checkboxElement).each(function(e){
console.log(e);
if($(this).is(":checked")){
console.log(boolcount+"true");
}
else {
console.log(boolcount+"flase");
}
})
为什么不直接访问必需的复选框?
这里有一个简单的示例来自动检查它们:
$(".checkboxes_required").each(function() {
$(this).children().each(function () {
$(this).attr("checked", true);
});
});
您可以使用相同的逻辑进行其他操作:
var boolcount = 0;
$(".checkboxes_required").each(function() {
$(this).children().each(function () {
if ($(this).is(":checked")) {
boolcount++;
console.log(boolcount + "true");
} else {
console.log(boolcount + "false");
}
});
});