如何将一个元素传递给循环中的jQuery帖子 [英] How to pass an element to jQuery post inside a loop

查看:77
本文介绍了如何将一个元素传递给循环中的jQuery帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

for (_field in _fields) {
    $.post(
        '/api/fields',
        { id: _field.id },
        function(data, _field) {
            alert(data);
        } (data, _fields[_field)
    );
}

我必须通过 _fields [_field] 返回jQuery数据的函数的元素,因为在循环期间丢失了对正确对象的引用。问题是,在定义 post 函数应该有一个 _field 参数时,你还必须指定一个参数数据或数据将被 _field 覆盖。

I have to pass the _fields[_field] element to the function that returns the data from the jQuery because loses the reference to the right object during the loop. The problem is that in defining that the post function should have a _field parameter, you also have to specify a parameter for data, or data will be overwritten with _field.

目前数据返回 undefined 因为我没有在循环中定义数据对象。我也尝试传入 null ,但这也只是返回 null 。我正在寻找一种方法来传递元素而不会覆盖从post函数返回的数据。

Currently data returns as undefined because I have no data object defined inside the loop. I also tried passing in null, but that also just returns null. I'm looking for a way to pass the element without overwriting the data returned from the post function.

有没有办法解决这个问题,或者是否有替代方法可以做所需的jQuery方法吗?

Is there any way to fix this, or is there perhaps an alternative jQuery method that can do what's needed?

推荐答案

你想要一个函数工厂函数 —一个创建函数的函数:

You want a function factory function — a function that creates a function:

for (_fieldName in _fields) {
    $.post('/api/fields',
    {
        // Unrelated, but I think this bit is wrong; shouldn't it be
        // `id: _fields[_fieldName].id` ? You're trying to use `.id` on
        // a string -- see below for a full update
        id: _fieldName.id
    },
    makeHandler(_fields[_fieldName])
    );
}
function makeHandler(field) {
    return function(data) {
        // Use `data` and `field` here
    };
}

请注意,在对象初始值设定项中,我们传入 $。发布,我们调用 makeHandler 运行它并返回我们将传入的函数 $。交。然后在 $。post 完成时调用该函数,并且可以访问 data 参数 $。post 将它与 makeHandler 字段参数一起提供,因为它是对 makeHandler 调用上下文的闭包,其中包含字段参数。更多: 闭包不复杂

Note that in the object initializer we're passing into $.post, we're calling makeHandler to it runs and returns the function we'll then pass into $.post. That function is then called when the $.post completes, and has access to the data argument that $.post gives it as well as the field argument to makeHandler, because it's a closure over the context of the call to makeHandler, which includes the field argument. More: Closures are not complicated

请注意,在上面的代码中,我更改了变量 _field _fieldName 更清楚: for..in 循环中的变量是一个字符串,名称属性。另请参阅评论,我认为您试图在错误的地方使用 .id 。这是我认为你真正想要的:

Note that in the code above, I changed your variable _field to _fieldName to be more clear: The variable in for..in loops is a string, the name of a property. See also the comment, I think you were trying to use .id in the wrong place. Here's what I think you really wanted:

for (_fieldName in _fields) {
    _field = _fields[_fieldName];
    $.post('/api/fields',
    {
        id: _field.id
    },
    makeHandler(_field)
    );
}
function makeHandler(field) {
    return function(data) {
        // Use `data` and `field` here
    };
}

另请注意,如果 _fields 是一个数组,你不应该在没有安全措施的情况下使用 for..in 。更多: <$ c $的神话和现实c> for..in

Also note that if _fields is an array, you shouldn't use for..in on it without safeguards. More: Myths and realities of for..in

这篇关于如何将一个元素传递给循环中的jQuery帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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