简体   繁体   English

使用 Javascript/jQuery 切换 > 10000 个复选框的最快、最有效的方法是什么?

[英]What's the fastest, most efficient way to toggle > 10000 checkboxes with Javascript/jQuery?

I have a div with more than 10000 checkboxes in it.我有一个包含超过 10000 个复选框的div

For now, take it as a technical limitation that I have 10000 checkboxes.现在,将它作为技术限制,我有 10000 个复选框。 It can't and won't change.它不能也不会改变。 This is not a homework.这不是作业。

<div class="well well-sm" style="min-height: 100px; max-height: 360px; overflow: auto; background-color: #f7f7f7;">

        <label>
            <input type="checkbox" class="checkbox-item" name="some_name" value="28" checked="checked"> Item 1
        </label><br />

<!-- another 10000 of these -->
</div>

and this jQuery code that activates when specific buttons are pressed.这个 jQuery 代码在按下特定按钮时激活。

        $('.select-all').click(function () {
            $(this).parent().find(':checkbox:visible').prop('checked', true).change();
        });

        $('.unselect-all').click(function () {
            $(this).parent().find(':checkbox:visible').prop('checked', false).change();
        });

        $('.select-inverse').click(function () {
            $(this).parent().find(':checkbox:visible').click();
        });

(Ignore the :visible part as the list could be filtered) (忽略:visible部分,因为列表可以被过滤)

When the number of checkboxes is in the thousands the whole thing gets too slow, especially when ~10000 checkboxes need to be toggled.当复选框的数量达到数千时,整个过程变得太慢,尤其是当需要切换约 10000 个复选框时。

I was wondering if there's a faster (not necessarily better) way to toggle all of them at the same time (mostly for select/unselect all, as inverse selection can be removed altogether)我想知道是否有更快(不一定更好)的方式同时切换所有这些(主要用于全选/取消全选,因为反向选择可以完全删除)

Ok, I know how much you loved my 10000 checkboxes so since I recently moved to 50000 checkboxes I figured it would be fun to post an answer (maybe I'll get enough downvotes to keep me going till the ultimate target of 100000 checkboxes on a single page).好吧,我知道你有多喜欢我的 10000 个复选框,所以自从我最近搬到 50000 个复选框后,我认为发布一个答案会很有趣(也许我会得到足够多的反对票来让我继续实现 100000 个复选框的最终目标单页)。

I updated the HTML since last time to this:自上次以来,我将 HTML 更新为:

<div class="well  well-sm  top" data-textarea-target="name-of-target">
    <div class="checkbox-item">
        <input type="checkbox" name="some_name" id="28" value="28" checked="checked">
        <label for="28">
            <span class="well-text-style-1">Item 1</span>
            <span class="well-text-style-2">subtitle</span>
        </label>
    </div>
<!-- another 50000 of these -->
</div>

and this is the code for the buttons that can now manipulate 50000 checkboxes in 2-4 seconds:这是现在可以在 2-4 秒内操作 50000 个复选框的按钮的代码:


    $('.select-all').click(function () {
        // we check all visible checkboxes
        $(this).parent().find(':checkbox:visible').prop('checked', true);
        // other code here that calls functions to handle the removal of .change()
    });

    $('.unselect-all').click(function () {
        // we uncheck all visible checkboxes
        $(this).parent().find(':checkbox:visible').prop('checked', false);
        // other code here that calls functions to handle the removal of .change()
    });


    $('.select-inverse').click(function () {
        // we uncheck the visible, checked ones and check the visible, unchecked ones
        $(this).parent().find(':checkbox:visible').prop( "checked", function( i, val ) {
            return !val;
        });
        // other code here that calls functions to handle the removal of .click()
    });

The only thing that has changed is we removed the.change() and added code that replicates its functionality once all checkboxes have been manipulated.唯一发生变化的是我们删除了 the.change() 并添加了在操作完所有复选框后复制其功能的代码。

This has resulted in being able to check/uncheck 50000 checkboxes in as little as 1-2 seconds and 3-4 seconds on average.这导致能够在短短 1-2 秒和平均 3-4 秒内选中/取消选中 50000 个复选框。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多次使用jQuery选择器的最有效/最快的方法是什么? - What is the most efficient/fastest way to use a jQuery selector multiple times? 在IE中使用Javascript / jQuery管理大型数据集的最有效方法是什么? - What's the most efficient way to manage large datasets with Javascript/jQuery in IE? 加载数据时暂停setInterval的最简单/最有效的方法是什么? JavaScript / jQuery - What's the easiest / most efficient way to pause setInterval while data is loading? JavaScript/Jquery 从jQuery中的serializeArray获取值的最有效方法是什么? - What's the most efficient way to fetch the value from a serializeArray in jQuery? 使用JavaScript评估字符串是否是回文符的最有效方法是什么? - What's the most efficient way to evaluate if a string is a palindrome using Javascript? 在Java中通过字符串的最有效方式是什么? - What is the most efficient way to go through string's chars in Javascript? 在JavaScript中,将标签转换为链接的最有效方法是什么? - In javascript, what's the most efficient way to I turn tags into links? 比较 javascript 中的 2 个元素的最有效方法是什么? - What's the most efficient way to compare 2 elements in javascript? 在Javascript中创建嵌套div的最有效方法是什么? - What is the most efficient way to create nested div's in Javascript? 在 JavaScript 中处理显示对话框/模式的最有效方法是什么? - What's the most efficient way to handle displaying a dialog/modal in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM