当所有子复选框都选中/取消选中时,选中/取消选中主复选框
我目前正在向我的网站添加复选框并添加一些 jquery,以便当用户选中主题复选框时,它会检查所有子主题复选框。我已经使用下面的代码完成了这个工作。
我似乎无法开始工作的是,当用户选中所有子主题复选框时,它会检查主题复选框,而当用户取消选中子主题时,它会取消选中主题复选框。
任何帮助都会很棒
当前 jquery
$(document).ready(function () {
$(".topic input").click(function () {
$(this).parents("tr").find("input").prop('checked', (this.checked ? "checked" : ""));
})
Html
<div class="topic">
<input type="checkbox" name="topic" value="1">
<span>Topic 1</span>
</div>
<div class="subtopic">
<ul class="inputs-list">
<li>
<input type="checkbox" checked="" name="subtopic" value="1">
<span>subtopic 1</span>
</li>
<li>
<input type="checkbox" checked="" name="subtopic" value="2">
<span>subtopic 2</span>
</li>
<li>
<input type="checkbox" checked="" name="subtopic" value="3">
<span>subtopic 3</span>
</li>
</ul>
</div>
I am currently working on adding checkboxes to my website and adding some jquery so when the user checks the topic checkbox, it checks all the subtopic checkboxes. I have got this to working using the code below.
What I can not seems to get to work is when the user checks all the subtopic checkboxes it checks the topic checkbox and when the user uncheck a subtopic it unchecks the topic checkbox.
Any help would be great
Current jquery
$(document).ready(function () {
$(".topic input").click(function () {
$(this).parents("tr").find("input").prop('checked', (this.checked ? "checked" : ""));
})
Html
<div class="topic">
<input type="checkbox" name="topic" value="1">
<span>Topic 1</span>
</div>
<div class="subtopic">
<ul class="inputs-list">
<li>
<input type="checkbox" checked="" name="subtopic" value="1">
<span>subtopic 1</span>
</li>
<li>
<input type="checkbox" checked="" name="subtopic" value="2">
<span>subtopic 2</span>
</li>
<li>
<input type="checkbox" checked="" name="subtopic" value="3">
<span>subtopic 3</span>
</li>
</ul>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,您有两个事件:
如果检查了子主题,则从主题中删除检查
So you have two events:
remove checked from topic if subtopic is checked
第一个更改事件处理程序用于主题选择,第二个更改用于子主题取消选中
The first change event handler for the topic select and the second change is for the subtopic uncheck