Pergunta

I put a checkbox 'Check All', and my code only works first all check and all uncheked. After that none of the checked box follows the attribute of the checkbox of 'Check All'

<script type="text/javascript">
    $(function () {  
        $('#chkAll').click(function () {
            $('#divChk .chk input:checkbox').attr('checked', function () {
                return $('#chkAll').prop('checked');
            });
        });
    });
</script>


<body>
    <form id="form1" runat="server">
        <div class="bigDiv">
            <asp:CheckBox ID="chkAll" runat="server" Text="Check All" /><br />
            <div id="divChk">
                <asp:CheckBox ID="CheckBox1" runat="server" Text="1" CssClass="chk" /><br />
                <asp:CheckBox ID="CheckBox2" runat="server" Text="2" CssClass="chk" /><br />
                <asp:CheckBox ID="CheckBox3" runat="server" Text="3" CssClass="chk" /><br />
                <asp:CheckBox ID="CheckBox4" runat="server" Text="4" CssClass="chk" /><br />
            </div>
        </div>
    </form>
</body>
Foi útil?

Solução

Try this and see if this works.

   $('#chkAll').change(function () {
            $('#divChk input:checkbox').prop('checked',this.checked);
    });
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top