How can I shift-select multiple checkboxes like GMail?

后端 未结 13 1083
野趣味
野趣味 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:50

    • Found the better solution it works for both select and deselects checkboxes.

    • Uses a core javascript & Jquery.

    $(document).ready(function() {
        var $chkboxes = $('.chkbox');
        var lastChecked = null;
    
        $chkboxes.click(function(e) {
            if(!lastChecked) {
                lastChecked = this;
                return;
            }
    
            if(e.shiftKey) {
                var start = $chkboxes.index(this);
                var end = $chkboxes.index(lastChecked);
    
                $chkboxes.slice(Math.min(start,end), Math.max(start,end)+ 1).prop('checked', e.target.checked);
    
            }
    
            lastChecked = this;
        });
    });
    
    
        
        
        
            Check 1
    Check 2
    Check 3
    Check 4
    Check 5
    Check 6
    Check 7

提交回复
热议问题