TypeError:< Array> .each不是函数 [英] TypeError: <Array>.each is not a function

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

问题描述

我在页面上有三个对三个下拉列表的引用,并且每个都被更改,我想运行一个名为 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()
});
});
});

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


TypeError:formLevel2DDs.each不是函数


我使用的是jQuery版本1.8.3 (这是遗留系统)。

解决方案

每个没有数组上的函数。



Anton 在评论中指出,你根本不需要每个来做你正在做的事情;请看下面的折扣。



但如果你想要每个,你有三个选择:


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


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



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


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



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







但是对于Anton来说,你可以更简单地做到这一点:



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

  jQuery(文档)。 ready(function(){

$(#supplier,#formElementChargeRate,#formElementEmailIDS)。change(validateForm);

});

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



如果你真的想使用这些变量直接访问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);
});


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

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

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

解决方案

There is no each function on arrays.

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

But if you want each, you have three choices:

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


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

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

});

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

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> .each不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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