jQuery - Ajax成功参数作为对象函数

时间:2009-11-16 05:24:24

标签: jquery

jQuery - ajax成功参数 我想知道如何在成功参数上给对象方法(函数)一个ajax函数(对不起我的英文)

假设我已经设置了我的ajax.setup,我只需要在服务器上调用php。

function myClass(id)
{
  this.id = id;

  this.showValues=function(xml) 
  {
    alert(this.id); // to prove we r in the right object
    alert(typeof(xml)); // to prove we got the xml     
  };

  this.retrieveValues=function()
  { 
    // wont call the method, this is just text, like writing sucess:"hello"
    $.ajax({success: this.id+'.showValues'}); 


   // calls the method but this.id is undefined (xml is received tho)
    $.ajax({success: this.showValues}); 


    // wont call the method
    $.ajax({success: new Function(this.id+'.showValues')}); 


    // calls the method with the right id but xml is undefined
    $.ajax({success: new Function(this.id+'.showValues()')}); 


    // js error - "xml not defined"
    $.ajax({success: new Function(this.id+'.showValues(xml)')}); 


    // of course, next line works, but i dont wanna define the function here
     $.ajax({success: function() { alert(this.id); alert(typeof(xml)); } });    

  };

}


// even declaring the object as global, so the class has a chance to call a method using the var name
// wich is this.id+'.retrieveValues' ==> object1.retrieveValues
var object1;

function Main() // main =d
{
  object1 = new myClass('object1');
  object1.retrieveValues();

}

再次,抱歉我的英语不好..学习仍然= d 我希望有人可以帮助我,谢谢你

1 个答案:

答案 0 :(得分:1)

使用Function api help< _<我错过了“xml”参数

$.ajax({success: new Function("xml",this.id+'.showValues(xml)')}); 

new Function(“xml”,this.id +'。showValues(xml)')就像说

功能(XML)    {object1.showValues(xml);    }

谢谢dedoz。 没问题。 = d