javascript - 无法清空 JS 数组

标签 javascript jquery arrays

是的,我对这个非常基本的(或者看起来如此)的事情有疑问。我对 JS 还很陌生,并且仍在尝试了解它,但我对 PHP 非常熟悉,并且从未经历过这样的事情。我只是无法清空这个该死的数组,而且每次我运行这个数组时,东西都会被添加到末尾。

我不知道为什么,我开始认为它在某种程度上与 chekbox id 的命名方式有关,但我可能错了......

id="别名[1321-213]", id="别名[1128-397]", id="alias[77-5467]" 等等。

我试过坚持

复选框 = [];checkboxes.length = 0;

在每个可能的地方。就在函数开始之后,在函数结束时,甚至在函数之外,就在函数之前,但这没有帮助,清空这个数组的唯一方法是重新加载页面。请告诉我我做错了什么,或者至少指出我可以 RTFM 的地方。我完全没有想法了。

function() {

    var checkboxes = new Array();
    checkboxes = $(':input[name="checkbox"]');

        $.each(checkboxes,
            function(key, value) {
                     console.log(value.id);                            
                     alert(value.id);
          }
        );
     checkboxes.length = 0;                          
}

我还读过Mastering Javascript Arrays 3次以确保我没有做错什么,但仍然无法弄清楚......

最佳答案

我认为这会产生很多困惑,因为正在清除数组 - 只是可能没有达到您想要的目的,或者在错误的时间等

function () {
    var checkboxes = new Array();               // local-scope variable
    checkboxes = $(':input[name="checkbox"]');  // new instance

    $.each(checkboxes, /* ... */);              // (truncated for brevity)

    checkboxes.length = 0;                      // this clears the array
                                                // but, for what...?
                                                // nothing happens now
}

根据您的代码片段,每次调用该函数都会重新创建并清除数组。但是,所有对数组的操作都是在数组已满时完成的(清除后什么也不会发生)。

此外,checkboxes 是函数的私有(private)变量。该变量仅在函数执行期间存在,函数完成后就会被遗忘。

那么,总体情况是什么? 为什么要尝试清除数组?

<小时/>

猜测一下,听起来您打算在下次调用该函数时清除它。
即(填写doSomething作为函数名称):

doSomething(); // log all array elements and clears the array
doSomething(); // does nothing, since the array is already empty

要实现此目的,您需要在函数外部的单个位置定义复选框,无论是作为全局变量还是使用闭包(更推荐的选项,尽管更复杂):

NOTE: If you haven't dealt with closures before, they may not make much sense after only a single example. But, there are thousands of resources available, including Stack Overflow, to help explain them better than I can here.

// closure (or instantly-called function)
// allows for defining private variables, since they are only known by
// code blocks within this function

(function () {
    // private (closure-scoped) variable(s)
    var checkboxes = $(':input[name="checkbox"]');

    function doSomething() {
        $.each(checkboxes, /* ... */);

        checkboxes.length = 0;
    }
})();

闭包将运行一次,将 checkboxes 定义为输入数组,而 doSomething 将在清除数组之前迭代该数组。

现在,最后一步是公开 doSomething——因为与复选框一样,它也是私有(private)的。您可以通过将函数引用从闭包传递到闭包外部的变量来完成公开它:

 var doSomething = (function () {
     /* ... */

     return doSomething;  // note that you DO NOT want parenthesis here
 })();

关于javascript - 无法清空 JS 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2670614/

相关文章:

javascript - 如何使用 javascript 找出模式对话框的宽度和高度?

javascript - 如何使用给定行的 html 表中的输入字段创建动态行

javascript - Web 应用程序中具有自动淡入/淡出功能的 iPhone 样式滚动条

java - 将二维数组变量加载到类实例中

arrays - Delphi中的数组(对象帕斯卡)使用变量?

javascript - 如何通过 javascript 设置浏览器选项卡的 anchor 目标名称?

javascript - 仅跟踪 iframe 历史记录

javascript - 如何使用 jQuery 将两个 html 包含到另一个 html 中

javascript - jQuery ie 粘贴时出现问题(带有中断的内容)

java - Eclipse 给出关于不返回整数的错误