开发者

jQuery - unselecting checkbox if conditions match

开发者 https://www.devze.com 2023-02-04 18:18 出处:网络
I have a bit of a problem with some html im writing and was wondering if someone could help. I have 6 checkboxes which are the following

I have a bit of a problem with some html im writing and was wondering if someone could help.

I have 6 checkboxes which are the following

Monday Tuesday Wednesday Thursday Friday All Days

HTML is as follows:

<input type="checkbox" name="monday" value="1" id="monday" />
<input type="checkbox" name="tuesday" value="1" id="tuesday" />
<input type="checkbox" name="wednesday" value="1" id="wednesday" />
<input type="checkbox" name="thursday" value="1" id="thursday" />
<input type="checkbox" name="friday" value="1" id="friday" />
<input type="checkbox" name="alldays" value="1" id="alldays" />

I already have some jquery which when "All Days" is selected, checks all the rest of the checkboxes.

The jQuery i have at the moment is as follows:

                    <script>
                    $(function(){
                        $("#alldays").change(function(){
                            if (this.checked) {
                                $("input:checkbox.child").attr("checked", "checked");
                            }
                            else {
                                $("input:checkbox.child").removeAttr("checked");
                            }
                        });
                    });
                </script>

I want to add another bit of jQuery, if all six of t开发者_如何学Che checkboxes are ticked, and someone clicks off one of the weekdays, the "All Days" checkbox will automatically uncheck.

Could someone help me out with this please.

Cheers


Try this:

$("#alldays").change(function(){
    $("input:checkbox.child").attr("checked", this.checked);
});

$("input:checkbox.child").change(function() {
    if ($("#alldays").attr("checked")) {
        $("#alldays").attr("checked", false);
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消