在 jQuery 中,如何根据另一个复选框打开和关闭复选框的子集

发布于 2024-10-17 22:10:16 字数 978 浏览 0 评论 0原文

我有一个用复选框显示的部门和细分列表,通过数据库动态创建,最终结果有点像这样:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯角 2024-10-24 22:10:16

像这样的事情应该可以解决问题。

<input type="checkbox" class="divisions" value="division1" />
<input type="checkbox" class="division1_departments" value="division1_departmen1" />
<input type="checkbox" class="division1_departments" value="division1_departmen2" />
<input type="checkbox" class="divisions" value="division2" />
<input type="checkbox" class="division2_departments" value="division2_departmen1" />
<input type="checkbox" class="division2_departments" value="division2_departmen2" />
<script type="text/javascript">
    $(document).ready(function () {
        $("input.divisions:checkbox").click(function () {
            $("input." + $(this).val() + "_departments:checkbox").attr("checked", $(this).is(':checked'));
        });
    });
</script>

Something like this should do the trick.

<input type="checkbox" class="divisions" value="division1" />
<input type="checkbox" class="division1_departments" value="division1_departmen1" />
<input type="checkbox" class="division1_departments" value="division1_departmen2" />
<input type="checkbox" class="divisions" value="division2" />
<input type="checkbox" class="division2_departments" value="division2_departmen1" />
<input type="checkbox" class="division2_departments" value="division2_departmen2" />
<script type="text/javascript">
    $(document).ready(function () {
        $("input.divisions:checkbox").click(function () {
            $("input." + $(this).val() + "_departments:checkbox").attr("checked", $(this).is(':checked'));
        });
    });
</script>
旧时浪漫 2024-10-24 22:10:16

如果将每个组包围在

中,则可以使用 siblings ()

<div>
  <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
</div>
<div>
  <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
</div>


jQuery(document).ready(function() {
    $('input[name="divisions\\[\\]"]').click(function() {
        $(this).siblings('input[type="checkbox"]').attr('checked',this.checked);
    });
})

If you surround each group in a <div> you can use the siblings()

<div>
  <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
</div>
<div>
  <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
</div>


jQuery(document).ready(function() {
    $('input[name="divisions\\[\\]"]').click(function() {
        $(this).siblings('input[type="checkbox"]').attr('checked',this.checked);
    });
})
梦纸 2024-10-24 22:10:16

如果您能够像这样构建标记:

<div class="division">
    <input name="divisions[]" type="checkbox" value "division1"/> Division One
        <div class="department">
        <input name="departments[]" type="checkbox" value "department1"/> Department One
        <input name="departments[]" type="checkbox" value "department2"/> Department Two
        </div>
</div>

以下代码应该执行您想要的操作:

$('.division input[name="divisions\\[\\]"]').click(function() { 
    $(this).find('.department input[type="checkbox"]').attr('checked', this.checked);
});

If you were able to structure your markup like this:

<div class="division">
    <input name="divisions[]" type="checkbox" value "division1"/> Division One
        <div class="department">
        <input name="departments[]" type="checkbox" value "department1"/> Department One
        <input name="departments[]" type="checkbox" value "department2"/> Department Two
        </div>
</div>

The following code should do what you want:

$('.division input[name="divisions\\[\\]"]').click(function() { 
    $(this).find('.department input[type="checkbox"]').attr('checked', this.checked);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文