javascript - Jquery:在动态类下查找复选框
问题描述
我有一个动态表格,下面是结构
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 但不知道如何将其作为选择器传递以进一步循环复选框
$(".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 checkedCount = 0;
var uncheckedCount = 0;
$(".checkboxes_required").each(function () {
$(this).children().each(function () {
if ($(this).is(":checked")) {
checkedCount++;
} else {
uncheckedCount++;
}
});
});
console.log("A total of " + checkedCount + " checked");
console.log("and of " + uncheckedCount + " unchecked");
推荐阅读
- vim - 如何使用 VimL 脚本向当前 Vim 缓冲区添加一些行?
- python - AttributeError:“矩形”对象没有属性“左”
- javascript - 我使用的是纯 JavaScript,但我继续收到以“不是函数”结尾的错误。如何做到这一点,以便我可以检测单词并做出相应的回复?
- android - Android 键盘在页面上移动内容
- html - 更改导航栏以居中标题折叠的屏幕宽度
- multi-touch - Java Robot 的多点触控手势
- github - Heroku GIT 部署失败 (yarn.lock)
- node.js - CredentialsProviderError:无法从任何提供者 nodejs aws 错误加载凭据
- python - 如何使用 winfo_children 修改在不同框架中网格化的 tkinter 按钮?
- cakephp - 如果在 cakephp 4 中对随机查找查询使用分页会得到意想不到的结果?