在 jQuery 中,如何根据另一个复选框打开和关闭复选框的子集
我有一个用复选框显示的部门和细分列表,通过数据库动态创建,最终结果有点像这样:
<input name="divisions[]" type="checkbox" value "division1"/> Division One <input name="departments[]" type="checkbox" value "department1"/> Department One <input name="departments[]" type="checkbox" value "department2"/> Department Two <input name="divisions[]" type="checkbox" value "division2"/> Division Two <input name="departments[]" type="checkbox" value "department3"/> Department Three <input name="departments[]" type="checkbox" value "department4"/> Department Four
我需要编写 jQuery 代码,这样当用户单击该部门的所有部门并且仅单击该部门时,我需要编写 jQuery 代码 。部门同样可以点击打开或关闭。
我正在考虑将单击事件函数附加到打开或关闭所有直接部门的所有部门,尽管我不知道语法: 从概念上我在想:
jQuery(document).ready(function() {
$(all_elements_with_the_name_divisions).click(function() {
$(all_immediately_following_elements_with_the_name_departments).attr('checked',this.checked);
});
})
谢谢
I have a list of divisions and subdivisions that I show with checkboxes, dynamically created through a database with the end result somewhat like this:
<input name="divisions[]" type="checkbox" value "division1"/> Division One <input name="departments[]" type="checkbox" value "department1"/> Department One <input name="departments[]" type="checkbox" value "department2"/> Department Two <input name="divisions[]" type="checkbox" value "division2"/> Division Two <input name="departments[]" type="checkbox" value "department3"/> Department Three <input name="departments[]" type="checkbox" value "department4"/> Department Four
I need to write the jQuery code such when the user clicks on the a division all of its departments and only its departments are equally clicked on or off.
I was thinking of attaching a clicked event function to all divisions that turned on or off all the immediate departments, though I don't know the syntax:
conceptually I was thinking:
jQuery(document).ready(function() {
$(all_elements_with_the_name_divisions).click(function() {
$(all_immediately_following_elements_with_the_name_departments).attr('checked',this.checked);
});
})
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的事情应该可以解决问题。
Something like this should do the trick.
如果将每个组包围在
中,则可以使用
siblings ()
If you surround each group in a
<div>
you can use thesiblings()
如果您能够像这样构建标记:
以下代码应该执行您想要的操作:
If you were able to structure your markup like this:
The following code should do what you want: