How can I shift-select multiple checkboxes like GMail?

后端 未结 13 1074
野趣味
野趣味 2020-11-30 17:33

In GMail, the user can click on one checkbox in the email list, hold down the Shift key, and select a second checkbox. The JavaScript will then select/unselect the checkboxe

13条回答
  •  悲哀的现实
    2020-11-30 17:46

    It seems like every answer I can find online is completely dependent on jQuery for this. JQuery adds very little functionality. Here's a quick version that doesn't require any frameworks:

    function allow_group_select_checkboxes(checkbox_wrapper_id){
        var lastChecked = null;
        var checkboxes = document.querySelectorAll('#'+checkbox_wrapper_id+' input[type="checkbox"]');
    
        //I'm attaching an index attribute because it's easy, but you could do this other ways...
        for (var i=0;ij){
                        var low = j; var high=i; 
                    }
    
                    for(var c=0;c

    And then initialize it whenever you need it:

    allow_group_select_checkboxes('[id of a wrapper that contains the checkboxes]')
    

提交回复
热议问题