Checkbox click script - [SHIFT] check / uncheck, [CTRL] check / uncheck all - based on the selected name?

Does anyone know of a ready-made script or plugin that provides:

-Shift click for check/uncheck all in range
-CTRL click to select or unselect all

This can work on behalf of the validation input (instead of everything on the page or everything inside the div):

input[name='user_group[]']
input[name='record_group[]']

I use a couple of scripts (javascript and jQuery), but they are based on all the checkboxes in a div or table, and I'm not smart enough to flip my own or change another script. Google's search on this subject was a bit complicated (there are too many general terms that I think) ...

Thank you, thank you very much!

+3
source share
2 answers

I started playing with this script, although it lacks the CTRL + Click function (select all / none control).

. "$ (" input [type = checkbox] "). ShiftClick();" linke to "$ (" input [ name= 'selected_employees []'] "). shiftClick();" , , .

( ) - CTRL + Click, .

<script type="text/javascript">
  $(document).ready(function() {
    // shiftclick:  http://sneeu.com/projects/shiftclick/
    // This will create a ShiftClick set of all the checkboxes on a page.
    $(function() {
        $("input[name='selected_employees[]']").shiftClick();
        // $('input[type=checkbox]').shiftClick();
    });

    (function($) {
        $.fn.shiftClick = function() {
            var lastSelected;
            var checkBoxes = $(this);

            this.each(function() {
                $(this).click(function(ev) {
                    if (ev.shiftKey) {
                        var last = checkBoxes.index(lastSelected);
                        var first = checkBoxes.index(this);

                        var start = Math.min(first, last);
                        var end = Math.max(first, last);

                        var chk = lastSelected.checked;
                        for (var i = start; i < end; i++) {
                            checkBoxes[i].checked = chk;
                        }
                    } else {
                        lastSelected = this;
                    }
                })
            });
        };
    })(jQuery);
  });
</script>
+2

, !

jsFiddle: http://jsfiddle.net/SXdVs/3/

var firstIndex = null;

$(":checkbox").click(function(e) {
    $this = $(this);
    if (e.ctrlKey) {
        if ($this.is(":checked")) {
          $("input[name='"+ $this.attr("name") +"']").attr("checked", "checked");
        } else {
          $("input[name='"+ $this.attr("name") +"']").removeAttr("checked");
        }
    } else if (e.shiftKey) {
        $items = $("input[name='"+ $this.attr("name") +"']");
        if (firstIndex == null) {
            firstIndex = $items.index($this);
        } else {
            var currentIndex = $items.index($this);
            var high = Math.max(firstIndex,currentIndex);
            var low = Math.min(firstIndex,currentIndex);

            if ($this.is(":checked")) {
                $items.filter(":gt("+ low +"):lt("+ high +")").attr("checked", "checked");
            } else {
                $items.filter(":gt("+ low +"):lt("+ high +")").removeAttr("checked");
            }

            firstIndex = null;
        }
    } 
});
-1

All Articles