TypeError:< Array& gt ;.每个都不是函数 [英] TypeError: <Array>.each is not a function

查看:96
本文介绍了TypeError:< Array& gt ;.每个都不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有3个下拉菜单的3个引用,并且每次更改时,我都想运行一个名为validateForm();

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();

我的代码如下:

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不是一个函数

TypeError: formLevel2DDs.each is not a function

我正在使用jQuery 1.8.3版(这是旧系统).

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

推荐答案

数组上没有each函数.

安东在评论中指出,您完全不需要each正在做;看到下面的褶皱.

As Anton points out in the comments, you don't need each at all for what you're doing; see below the fold.

但是,如果您想要each,则有三种选择:

But if you want each, you have three choices:

  1. 将数组包装到jQuery实例中,并使用jQuery的 each :

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

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

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

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

Note that this is not the same function as above.

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

请注意,forEach是ECMAScript5的新功能.所有现代的浏览器都具有此功能,但对于较旧的浏览器(如IE8),则需要使用填充程序/填充.还要注意,回调参数的顺序与上面的两个选项都不相同.

Note that forEach is 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.


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


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

在这种情况下,没有理由直接使用getElementById,它没有出现任何紧密循环,所以:

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

jQuery(document).ready(function() {

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

});

请注意,我还从validateForm附近删除了包装器功能.如果validateForm具有返回值,并且您不希望该返回值供jQuery使用,则可能需要将其添加回去(特别是:如果返回了false,则jQuery将停止传播并阻止的默认操作change事件).

Note that I've also removed the wrapper function from around validateForm. You may need to add it back if validateForm has 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 change event).

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

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

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);
});

这篇关于TypeError:< Array& gt ;.每个都不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆