开发者

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

开发者 https://www.devze.com 2023-02-17 03:45 出处:网络
Would anyone know of a ready-made script or plugin providing: -Shift click for check/uncheck all in range

Would anyone know of a ready-made script or plugin providing:

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

That can works off the check inputs 'name' (instead of all on a page or all inside a div):

input[name='user_group[]']
input[name='rec开发者_开发技巧ord_group[]']

I've been using a couple of scripts (javascript and jQuery) but they're based on all checkboxes in a div or table and I'm not smart enough to roll my own or modify another script. Google searching on this has been a little difficult (too many common terms I think)...

Thanks Much Appreciated!


I started playing around with this script, although it's missing a CTRL+Click feature (select all/none control).

In it's original form it works against all checkboxes on a page. I changed the "$('input[type=checkbox]').shiftClick();" linke to "$("input[name='selected_employees[]']").shiftClick();" and as far as I can tell it seems to be working perfectly now against only the single checkbox group.

The only flaw (for my requirements) is there is not a CTRL+Click function to toggle check or un-check all checkboxes in the group.

<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>


I believe this should work!

Working demo on 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;
        }
    } 
});
0

精彩评论

暂无评论...
验证码 换一张
取 消