将对象数据传递给jQuery.后继函数

Passing Object data to jQuery.post successor function

本文关键字:jQuery 函数 对象 数据      更新时间:2023-09-26

我要这个。类实例的项将由通过jQuery接收到的数据填充。后继函数。我可以使用另一个用户定义的函数来设置它。项,包含接收到的数据。

问题:有没有办法设置这个。项目在jquery .post()的后继函数没有使用任何其他用户定义的函数?

下面是代码片段

内部类原型函数:-

   this.item = new Array();
   jQuery.post("index.php?p=getdataitem", fileobj,function(item_str_data)
              {
        ...
        this.item = ....;   
                ...
              }
   );

谢谢

你可以做

 this.item = new Array();
var instance = this;
   jQuery.post("index.php?p=getdataitem", fileobj,function(item_str_data)
              {
        ...
        instance.item = ....;   
                ...
              }
   );

 this.item = new Array();
   jQuery.ajax(
         {url: "index.php?p=getdataitem", 
          context: this,
          success: function(item_str_data)
              {
                ...
                this.item = ....;   
                ...
              }
         }
   );

试试这个:

  var that = this;
  var that.item = [];
  function() {
      jQuery.post("index.php?p=getdataitem", fileobj,function(item_str_data) {
        ...
        that.item = ....;   
        ...
      });
   }();

由于闭包,我们复制的this引用(that)应该对post的内部函数可用。