我怎么可以参考jQuery的对象? [英] How can i refer the object on jQuery?

查看:114
本文介绍了我怎么可以参考jQuery的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在做这里面有一个1 Ajax调用每个功能的脚本。问题是,从Ajax调用成功的回调,我想用,我们在每一个功能的使用对象。

编辑: 下面是我的一些code:

 的configs = {
  一般:{
    max_ads:6},
  日志:{
    选择:#DIV MicrodualGetAd调试},
  连接:{
    键入:POST,
    网址:http://www.microdual.com/api/microdualgetad
    缓存:假的,
    超时:20000,
    数据类型:JSON,
    数据:{
      adget_id:空,
      adget_session:空,
      client_action:空},
    错误:函数(R,S,E){
      MicrodualGetAdLog(Ajax的错误:+ S +\ N);
    }
  }
};


$(div.MicrodualGetAd)。每个(函数(){
  configs.connection.data.adget_id = $(本).attr(相对);
  configs.connection.data.client_action =观点;
  configs.connection.data.success =函数(D,S,R){
    $(本).hide();
    警报('OK');
    如果(d.hackattemp.status){
      MicrodualGetAdLog(MicrodualGetAd错误:+ d.hackattemp.msg +\ N);
      $(本).append(d.hackattemp.msg);
    }
  }
  $阿贾克斯(configs.connection);
});
 

解决方案

您需要保存的值只是在你的每个回调。

  $(#你好)。每个(函数(){
    VAR的=这一点;

    $阿贾克斯({
        网址:www.mysite.com/mypage
        成功:函数(D){
            hello.call(即);
        }
    });
})
 

此外,你应该检查 D 不同的价值;无论是

 如果(typeof运算Ð==='不确定'和;&安培; D!=='')...
 

或只是

 如果(D)...
 


修改1:这应该工作,假设你有越来越权价值的唯一问题

  $(div.MicrodualGetAd)。每个(函数(){
    变量$此= $(本);
    configs.connection.data.adget_id = $ this.attr(相对);
    configs.connection.data.client_action =观点;
    configs.connection.data.success =函数(D,S,R){
        $ this.hide();
        警报('OK');
        如果(d.hackattemp.status){
            MicrodualGetAdLog(MicrodualGetAd错误:+ d.hackattemp.msg +\ N);
            $ this.append(d.hackattemp.msg);
        }
    }
    $阿贾克斯(configs.connection);
});
 


I'm making a script which has one ajax call inside an each function. The problem is that on the success callback from ajax call, I want to use the object that we are using at each function.

EDIT: Here is some of my code:

configs={
  general:{
    max_ads:6},
  logs:{
    selector:"div#MicrodualGetAd-debug"},
  connection:{
    type:"POST",
    url:"http://www.microdual.com/api/microdualgetad",
    cache:false,
    timeout:20000,
    dataType:"json",
    data:{
      adget_id:null,
      adget_session:null,
      client_action:null},
    error:function(r,s,e){
      MicrodualGetAdLog("Ajax-Error: "+s+"\n");
    }
  }
};


$("div.MicrodualGetAd").each(function(){
  configs.connection.data.adget_id = $(this).attr("rel");
  configs.connection.data.client_action = "view";
  configs.connection.data.success = function(d,s,r){
    $(this).hide();
    alert("'OK'");
    if(d.hackattemp.status){
      MicrodualGetAdLog("MicrodualGetAd-Error: "+d.hackattemp.msg+"\n");
      $(this).append(d.hackattemp.msg);
    }
  }
  $.ajax(configs.connection);
});

解决方案

You need to save the value of this just inside your each callback.

$("#hello").each(function() {
    var that = this;

    $.ajax({
        url: "www.mysite.com/mypage",
        success: function(d) {
            hello.call(that);
        }
    });
})​

Also, you should check the value of d differently; either

if (typeof d === 'undefined' && d !== '') ...

or simply

if (d) ...


Edit 1: This should work, assuming the only problem you're having is getting the right value of this:

$("div.MicrodualGetAd").each(function() {
    var $this = $(this);
    configs.connection.data.adget_id = $this.attr("rel");
    configs.connection.data.client_action = "view";
    configs.connection.data.success = function(d, s, r) {
        $this.hide();
        alert("'OK'");
        if (d.hackattemp.status) {
            MicrodualGetAdLog("MicrodualGetAd-Error: " + d.hackattemp.msg + "\n");
            $this.append(d.hackattemp.msg);
        }
    }
    $.ajax(configs.connection);
});​

这篇关于我怎么可以参考jQuery的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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