javascript TypeError: <Array>.each 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21164524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:15:24  来源:igfitidea点击:

TypeError: <Array>.each is not a function

javascriptjquery

提问by J86

I have three references to three drop downs on my page, and as each one is changed, I want to run a JavaScript function called validateForm();

我的页面上有三个对三个下拉菜单的引用,随着每个下拉菜单的更改,我想运行一个名为的 JavaScript 函数 validateForm();

My code is below:

我的代码如下:

jQuery(document).ready(function() {

    var drpSupplier         = document.getElementById('supplier');
    var drpChargeRate       = document.getElementById('formElementChargeRate');
    var drpIDSEmail         = document.getElementById('formElementEmailIDS');
    var formLevel2DDs       = new Array();

    formLevel2DDs.push(drpSupplier);
    formLevel2DDs.push(drpChargeRate);
    formLevel2DDs.push(drpIDSEmail);

    formLevel2DDs.each(function() {
        $(this).change(function() {
            validateForm()
        });
    });
});

But this code is giving me the error:

但是这段代码给了我错误:

TypeError: formLevel2DDs.each is not a function

类型错误:formLevel2DDs.each 不是函数

I am using jQuery version 1.8.3 (it is a legacy system).

我使用的是 jQuery 1.8.3 版(它是一个遗留系统)。

回答by T.J. Crowder

There is no eachfunction on arrays.

each数组没有函数。

As Antonpoints out in the comments, you don't need eachat all for what you're doing; see below the fold.

正如Anton在评论中指出的那样,你根本不需要each你在做什么;见折叠下方。

But if you want each, you have three choices:

但如果你愿意each,你有三个选择:

  1. Wrap your array in a jQuery instance and use jQuery's each: $(formLevel2DDs).each(function(index, entry) { ... });

  2. Use jQuery's $.each: $.each(formLevel2DDs, function(index, entry) { ... });

    Note that this is not the same function as above.

  3. Use forEach(MDN| Spec): formLevel2DDs.forEach(function(entry, index, array) { ... });

    Note that forEachis new as of ECMAScript5. All modern browsers have it, but you'll need a shim/polyfill for older ones (like IE8). Also note that the order of the arguments to the callback is different than either of the options above.

  1. 将您的数组包装在 jQuery 实例中并使用 jQuery 的each$(formLevel2DDs).each(function(index, entry) { ... });

  2. 使用 jQuery 的$.each$.each(formLevel2DDs, function(index, entry) { ... });

    请注意,这与上面的功能不同。

  3. 使用forEachMDN|规范):formLevel2DDs.forEach(function(entry, index, array) { ... });

    请注意,这forEach是 ECMAScript5 的新内容。所有现代浏览器都有它,但您需要为旧浏览器(如 IE8)添加一个 shim/polyfill。另请注意,回调参数的顺序与上述任一选项不同。



But to Anton's point, you can do that much more simply:

但就安东而言,您可以更简单地做到这一点:

There's no reason to use getElementByIddirectly in this case, it's not in a tight loop or anything, so:

getElementById在这种情况下没有理由直接使用,它不是紧密循环或任何东西,所以:

jQuery(document).ready(function() {

    $("#supplier, #formElementChargeRate, #formElementEmailIDS").change(validateForm);

});

Note that I've also removed the wrapper function from around validateForm. You may need to add it back if validateFormhas a return value, and you don't want that return value to be used by jQuery (specifically: if it returned false, jQuery would stop propagation and prevent the default action of the changeevent).

请注意,我还从 around 中删除了包装器函数validateForm。如果validateForm有返回值,您可能需要将其添加回来,并且您不希望 jQuery 使用该返回值(特别是:如果它返回false,jQuery 将停止传播并阻止change事件的默认操作)。

If you really wanted to have direct access to the DOM elements using those variables:

如果您真的想使用这些变量直接访问 DOM 元素:

jQuery(document).ready(function() {

    var drpSupplier, drpChargeRate, drpIDSEmail;
    var formLevel2DDs       = [
        drpSupplier         = document.getElementById('supplier'),
        drpChargeRate       = document.getElementById('formElementChargeRate'),
        drpIDSEmail         = document.getElementById('formElementEmailIDS')
    ];

    $(formLevel2DDs).change(validateForm);
});

回答by Anton

If you want to use .each()you must wrap the array with jQuery like this

如果你想使用.each()你必须像这样用jQuery包装数组

$(formLevel2DDs).each(function() {

It's not necessary to use a loop in this case, just use .change()on the array wrapped with jQuery

在这种情况下没有必要使用循环,只需.change()在用 jQuery 包装的数组上使用

$(formLevel2DDs).change(function(){
       validateForm()
});

回答by Pointy

The native iterator function is forEach, not each.

本机迭代器函数是forEach,不是each

The jQuery .eachfunction takes just one argument, that being the callback. The callback function is passed two parameters: the index into the list, and the value. The callback is invoked such that the list value is also the thisvalue.

jQuery.each函数只接受一个参数,即回调。回调函数传递两个参数:列表中的索引和值。调用回调使得列表值也是this值。

With forEach, the parameters are passed in reverse order: the value is first, followed by the index. The native function also passes the entire array as the third parameter. The native function does not bind thiswhen the callback is invoked unless a second parameter is passed to .forEachafterthe callback function. If there is such a parameter, it is used as the value of thisin the callback.

使用forEach,参数以相反的顺序传递:首先是值,然后是索引。本机函数还将整个数组作为第三个参数传递。this除非在回调函数.forEach之后传递第二个参数,否则在调用回调时不会绑定本机函数。如果有这样的参数,则作为this回调中的值。

The native iterator skips elements of the array that have not been set. The jQuery .each()does not skip such elements, instead always iterating from index 0 to length - 1.

本机迭代器跳过尚未设置的数组元素。jQuery.each()不会跳过此类元素,而是始终从索引 0 到length - 1.

回答by andy mccullough

try

尝试

$(formLevel2DDs).each(function() {
    $(this).change(function() {
        validateForm()
    });
});

回答by Vlad Nikitin

added one line

添加了一行

jQuery(document).ready(function() {

    var drpSupplier         = document.getElementById('supplier');
    var drpChargeRate       = document.getElementById('formElementChargeRate');
    var drpIDSEmail         = document.getElementById('formElementEmailIDS');
    var formLevel2DDs       = new Array();

    formLevel2DDs.push(drpSupplier);
    formLevel2DDs.push(drpChargeRate);
    formLevel2DDs.push(drpIDSEmail);

    formLevel2DDs = jQuery(formLevel2DDs);//this line
    formLevel2DDs.each(function() {
        $(this).change(function() {
            validateForm()
        });
    });
});

回答by mike

use a for ... in loop

使用 for ... in 循环

for(key in formLevel2DDs) { ... }