【问题标题】:Check/Uncheck inputs depending on parent/child input status根据父/子输入状态检查/取消选中输入
【发布时间】:2011-11-11 16:52:29
【问题描述】:

为了合乎逻辑,我需要选中/取消选中父子输入:

[x] parent [ ] child1 [x] child2

在这个例子中,如果我取消选中child2parent 也需要取消选中。而且,如果我取消选中parent,则无法再选中child2

[ ] parent [ ] child1 [ ] child2

在这种情况下,如果我检查child2,那么parent也必须检查。

我认为这很合乎逻辑,如果不检查其父级就无法检查一个子级,如果我取消选中一个子级(并且这是唯一一个选中的子级),则父级也必须取消选中。

我正在研究 PHP,所以任何与 Javascript (jQuery) 的集成都适合我。

【问题讨论】:

  • 你的 HTML 是什么样的?你试过什么?
  • 没有你的 html 你无法真正回答这个问题。
  • HTML 在这里无关紧要...想象一下只有 3 个输入,一个带有 class="parent",另外两个带有 class="children"
  • @scumah - 它对你的 jQuery 选择器有很大的影响。你打算使用 .parent()、.find() 吗?还有什么?
  • @mrtsherman,肯定会有所不同,但是,在我看来,这里重要的是逻辑,只是逻辑,而不是我们将使用哪个选择器来查找输入。

标签: jquery input checkbox


【解决方案1】:

这可能是一个很好的起点:

<form id="theform">
    <label>Parent</label><input name="group1" type="checkbox" value="parent1" class="parent" />
    <label>Child 1</label><input name="group1" type="checkbox" value="child1" class="child" />
    <label>Child 2</label><input name="group1" type="checkbox" value="child2" class="child" />
    <label>Child 3</label><input name="group1" type="checkbox" value="child3" class="child" />
</form>

<script type="text/javascript">
    $(document).ready(function(){
        $('#theform input:checkbox').change(function(){
            // Parent Checkbox Change //
            if ($(this).hasClass('parent')){
                if (!$(this).is(':checked')){
                    $('input.child[name="'+$(this).attr('name')+'"]').removeAttr('checked');
                }
            }
            // Child Checkbox Change //
            else if ($(this).hasClass('child')){
                if ($(this).is(':checked')){
                    $('input.parent[name="'+$(this).attr('name')+'"]').attr('checked', 'checked');
                } else {
                    if ($('input.child[name="'+$(this).attr('name')+'"]:checked').length == 0){
                        $('input.parent[name="'+$(this).attr('name')+'"]').removeAttr('checked');
                    }
                }   
            }
        });
    });
</script>

在这里提琴:http://jsfiddle.net/pjZnZ/3/

我希望这会有所帮助!

【讨论】:

  • 编辑为仅在取消选中所有子输入时取消选中父项。
  • 非常感谢!!两种代码都有效,但我选择了 dSquared 解决方案,因为孩子检查父输入而不是不允许检查它,我发现这会很有用。再次感谢!
【解决方案2】:

检查这个小提琴:http://jsfiddle.net/WvYgB/1/

jQuery:

$('.parent').change(function(){
    if (!($(this).is(':checked'))) {
        $('.children').attr('checked', false);
    }
});

$('.children').change(function(){
    if($(this).is(':checked') && !$('.parent').is(':checked')) {
        $(this).attr('checked', false);
    }
    if (!($(this).is(':checked') || $(this).siblings('.children').is(':checked'))) {
        $('.parent').attr('checked', false);
    }
});

【讨论】:

  • 小错误 - 在检查父母之前,您无法检查孩子。这是他的规范。
  • @mrtsherman,您在哪个浏览器中寻找小提琴?它对我有用:(
  • Chrome 15、IE9 和 FF8。问题是您的第一个 if 语句。它说,如果已选中 && 未选中父级,则删除该检查。它应该检查其父级,而不是取消检查自身。
  • 还有一点,在这种情况下您对attr 的使用是不正确的。您应该在 jQuery 1.7 中使用 prop 或以不同的方式使用 attr。 attr 的值为 'checked' 或 ''。不真不假。 Prop 有 true 和 false 的值。 jsfiddle.net/fGdAY
  • 你是对的,但使用 prop('checked')attr('checked') 将不起作用,因为输入可能没有 checked 属性,这会使事情变得有点混乱。检查jQuery's prop() page,演示向我们展示了这样的输入:&lt;input id="check1" type="checkbox" checked="checked"&gt;,这是不正确的,但它似乎不起作用。这是一个示例:jsfiddle.net/WvYgB/2。关于你注意到的小错误,我理解如果不检查父母,你就不能检查孩子,而不是父母也应该检查:jsfiddle.net/WvYgB/3
猜你喜欢
  • 2021-11-13
  • 2014-02-06
  • 2011-05-20
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
  • 1970-01-01
  • 2016-10-25
  • 2014-02-14
相关资源
最近更新 更多