无法从jQuery ajax响应中剥离img标记

时间:2013-05-07 22:03:33

标签: php javascript jquery html ajax

我有一个网络应用程序,它将一个ajax发布到PHP脚本,返回一堆html。在html中有一个图像和一个数据表。我想要做的是将图像与html的其余部分分开,以便我可以在其他地方显示它。

$.ajax({      
  type: "POST",
  url: 'api/service_connector.php',
  data: { selectedmodel: requestdata },
  beforeSend: function() {},
  success: function(returneddata) {                             
  var $response=$(returneddata);
  var test = $response.filter('.testimage').html();      
  alert(test);  
  }                     
});

如果在我的成功函数中我只是提醒整个返回的数据变量,一切正常。所以我知道这个帖子本身正在工作,一切都得到了妥善回报;它只是抓住我所面对的那些图像标签。我也试过了一些我在google上找到的其他“方法”。例如,

alert($(returneddata).find('.testimage').html());

3 个答案:

答案 0 :(得分:0)

尝试detach()

$.ajax({      
  type: "POST",
  url: 'api/service_connector.php',
  data: { selectedmodel: requestdata },
  beforeSend: function() {},
  success: function(returneddata) {                             
      var $response=$(returneddata);
      var $testimage = $response.find('.testimage');
      $testimage.detach();
      alert($('<p>').append($response).html());  
  }                     
});

http://jsfiddle.net/2H2FT/

答案 1 :(得分:0)

你玩过jquery的$.remove()吗?

例如:

<div class="container">
 <img class="theimage" src=".." />
</div>

...

$(".theimage").prop("src");   // get anything valuable from image first for future use
$(".theimage").remove();      // remove the image from there

答案 2 :(得分:0)

在php文件中

从表数据的第一个和最后一个添加分隔符('|')

在ajax功能

获取“|”之间的内容符号。

与图像标记一样