How can I shift-select multiple checkboxes like GMail?

后端 未结 13 1063
野趣味
野趣味 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 18:02

    This is jquery solution that I wrote and use:

    • All checkboxes have same class named chksel
    • For faster individual selection a class will carry the order named chksel_index
    • Also each checkbox has an attribute named rg that contain same index

      var chksel_last=-1;
      $('.chksel').click(function(ev){
         if(ev.shiftKey){var i=0;
            if(chksel_last >=0){
              if($(this).attr('rg') >= chksel_last){
               for(i=chksel_last;i<=$(this).attr('rg');i++){$('.chksel_'+i).attr('checked','true')}}
              if($(this).attr('rg') <= chksel_last){for(i=$(this).attr('rg');i<=chksel_last;i++){$('.chksel_'+i).attr('checked','true')}}
            }  
            chksel_last=$(this).attr('rg');
         }else{chksel_last=$(this).attr('rg');}
      

      })

提交回复
热议问题