2014-09-03 32 views
0

我可以从AJAX响应中多余一个td元素,但无法更改它。无法更改响应元素

$('#addjob').click(function(){ 
    $.ajax({ 
     type:"POST", 
     url: 'invoicelist.php', 
     success: function(response){ 
      $('#forajax').append(response); 
      $(response).find('.heading').html(); 
     } 
    }); 
}); 

此代码工作得很好,从<td class='heading'>123</td>选择文本,但如果我想改变这123结果我写$(response).find('.heading').html('456');但它并没有真正改变任何东西。 有什么建议吗?

回答

0

你写了原始HTML到您的#forajax容器,然后创建与response内容的新的jQuery对象。对新对象的任何更改都将被丢弃;他们与你写的HTML无关。

首先获取对象,对其进行修改,然后附加:

// create a jQuery object wrapping the returned HTML 
var r = $(response); 

// update the contents of our new object 
r.find('.heading').html('456'); 

// add the new, updated object to our container 
r.appendTo('#forajax'); 
+0

这工作真的很好......但我不明白为什么。你能再给我描述一次吗? – funny 2014-09-03 16:28:21

+0

在其中添加了评论 – 2014-09-03 16:37:54

0

更改它,然后追加。该内容不链接到该变量:

$(response).find('.heading').html('456'); 
$('#forajax').append(response); 
0

改变repsonse将在响应文本而不是附加的DOM对象改变。所以,而不是搜索dom元素,其中你追加和做那里

$('#addjob').click(function(){ 
    $.ajax({ 
     type: "POST", 
     url: 'invoicelist.php', 
     success: function(response){ 
      $('#forajax').append(response); 
      $('#forajax').find('.heading').html('456'); 
     } 
    }); 
});