The example in this article describes how to use shift click in JavaScript to select and deselect checkboxes. Share it with everyone for your reference. The specific implementation method is as follows:
var lastChecked = null; var handleChecked = function(e) { if(lastChecked && e.shiftKey) { var i = $('input[type="checkbox"]').index(lastChecked); var j = $('input[type="checkbox"]').index(e.target); var checkboxes = []; if (j > i) { checkboxes = $('input[type="checkbox"]:gt('+ (i-1) +'):lt('+(j-i)+')'); } else { checkboxes = $('input[type="checkbox"]:gt('+ j +'):lt('+ (i-j) +')'); } if (!$(e.target).is(':checked')) { $(checkboxes).removeAttr('checked'); } else { $(checkboxes).attr('checked', 'checked'); } } lastChecked = e.target; // Other click action code. } $('input[type=checkbox]').click(handleChecked);
I hope this article will be helpful to everyone’s JavaScript programming design.