简体   繁体   中英

Check all checkbox by clicking on 1 checkbox

  <asp:DataList runat="server" ID="dl1" OnItemDataBound="cb1">               
                  <ItemTemplate>
                    <div style="display: table;">
                        <div style="display: table-row;">
                            <div style="display: table-cell;">
                                <asp:CheckBox ID="Cb1"                 runat="server"/></div>
                            <div style="display: table-cell;">
                                <asp:CheckBox ID="cb2" runat="server" /></div>                                                       
                        </div>
                    </div>
                </ItemTemplate>
            </asp:DataList>

Here, I want a jquery/javascript where If I select cb1 then cb2 should be selected automatically and not vice versa.

I am trying this: But can anyone please help me.

<script type="text/javascript">

    var d1Control = document.getElementById('<%= dl1.ClientID %>');
    $('input:checkbox[id$=cb1]', d1Control ).click(function (e) {
        if (this.checked) {
            $('input:checkbox[id$=cb2]', d1Control ).attr('checked', true);
        }
        else {
            $('input:checkbox[id$=cb2]', d1Control ).removeAttr('checked');
        }
    });
</script>

Maybe this might help?

$('id$=cb1').click(function () {
    if ($(this).is("checked")) {
        $('id$=cb2').attr('checked' true);
    } else {
        $('id$=cb2').attr('checked', false);
    }
});

This is to have a "Select All" checkbox. Where you check one box and it selects all of the checkboxes for you.

   $('#cb1').click(function () {
            var selectAll = $("#cb1").is(":checked");
            if (selectAll)
            {
                $('input[type=checkbox]').attr("checked", true);
            }
            if (!selectAll)
            {
                $('input[type=checkbox]').attr("checked", false);
            }

        });

If you want it to only affect a particular checkbox (cb2):

      $('#cb1').click(function () {
            var selectAll = $("#cb1").is(":checked");
            if (selectAll)
            {
                $('id$=cb2').attr("checked", true);
            }
            if (!selectAll)
            {
                $('id$=cb2').attr("checked", false);
            }

        });
        $('#checkAll').click(function(){
            if($(this).is(':checked')){
                $('input[type=checkbox]').prop('checked', true)
            }else{
                $('input[type=checkbox]').prop('checked', false)
            }
        })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM