jquery select all checkboxes

前端 未结 19 2638
情书的邮戳
情书的邮戳 2020-12-13 12:36

I have a series of checkboxes that are loaded 100 at a time via ajax.

I need this jquery to allow me to have a button when pushed check all on screen. If more are lo

相关标签:
19条回答
  • 2020-12-13 13:32
    <div class="control-group">
    <input type="checkbox" class="selAllChksInGroup"> All
    <input type="checkbox" value="NE"> Nebraska
    <input type="checkbox" value="FL"> Florida
    </div>
    
    
    $(document).ready(function(){
    
    $("input[type=checkbox].selAllChksInGroup").on("click.chkAll", function( event ){
        $(this).parents('.control-group:eq(0)').find(':checkbox').prop('checked', this.checked);
    });
    
    });
    
    0 讨论(0)
  • 2020-12-13 13:34

    I know I'm revisiting an old thread, but this page shows up as one of the top results in Google when this question is asked. I am revisiting this because in jQuery 1.6 and above, prop() should be used for "checked" status instead of attr() with true or false being passed. More info here.

    For example, Henrick's code should now be:

    $(function () {
        $('#selectall').toggle(
            function() {
                $('#friendslist .tf').prop('checked', true);
            },
            function() {
                $('#friendslist .tf').prop('checked', false);
            }
        );
    });
    
    0 讨论(0)
  • 2020-12-13 13:34

    Here is how I achieved it.

    function SelectAllCheckBoxes();
    {
    $('#divSrchResults').find(':checkbox').attr('checked', $('#chkPrint').is(":checked"));
    }
    

    The following fires the above line.

    <input type=checkbox id=chkPrint onclick='SelectAllCheckBoxes();' /> 
    

    On the click of chkPrint , every checkbox in the grid divSrchResults' is either checked or unchecked depending on the status of chkPrint.

    Of course, if you need advanced functions like unchecking the titled checkbox when every other checkbox has been unchecked, you need to write another function for this.

    0 讨论(0)
  • 2020-12-13 13:35

    maybe try this:

    $(function () {
        $('#selectall').click(function () {
            $('#friendslist .tf').attr('checked', this.checked);
        });
    });
    
    0 讨论(0)
  • 2020-12-13 13:36

    This is how I toggle checkboxes

    $(document).ready(function() {
        $('#Togglebutton').click(function() {
            $('.checkBoxes').each(function() {
                $(this).attr('checked',!$(this).attr('checked'));
            });
        });
    });
    
    0 讨论(0)
  • 2020-12-13 13:38

    Use the jquery toggle function. Then you can also perform whatever other changes you may want to do along with those changes... such as changing the value of the button to say "check all" or "uncheck all".

    $(function () {
        $('#selectall').toggle(
            function() {
                $('#friendslist .tf').attr('checked', 'checked');
            },
            function() {
                $('#friendslist .tf').attr('checked', '');
            }
        );
    });
    
    0 讨论(0)
提交回复
热议问题