额外的参数的jQuery的Ajax成功功能 [英] Extra parameter for jQuery's ajax success function

查看:112
本文介绍了额外的参数的jQuery的Ajax成功功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个code读取XML文件:

I'm fetching an XML file using this code:

function getMaps(){

    toLoad = loadMaps.length;

    for (var i = 0; i < loadMaps.length; i++){
        $.ajax({
          type: "GET",
          url: loadMaps[i],
          dataType: "xml",
          success: processMap
        });
    }
}

,工作正常,但我想给processMap另一个参数(即loadMaps [我],在其中存储的加载XML中的名称)

Which works fine, but I want to give processMap another parameter (namely loadMaps[i], the name under which to store the loaded xml)

我无法弄清楚如何做到这一点,而不诉诸全局变量,这不是我想要的。

I can't figure out how to do this without resorting to global variables, which is not what I want.

推荐答案

jQuery的成功回调有三个参数,不能改变或扩展。所以,你需要一个匿名函数,它关闭了在调用你的函数。

The jQuery success callback has three parameters, which cannot be changed or expanded. So you need to call your function within an anonymous function which closes over.

for (var i = 0; i < loadMaps.length; i++){
    $.ajax({
      type: "GET",
      url: loadMaps[i],
      dataType: "xml",
      success: function(xhr, textStatus, error){
           processMap(loadMaps[i]);
      }
    });
}

这篇关于额外的参数的jQuery的Ajax成功功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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