无法更改响应元素

Cannot change response element

本文关键字:响应 元素      更新时间:2023-09-26

我可以从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');,但它并没有真正改变任何东西。有什么建议吗?

将原始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');

更改它,然后追加。该内容与变量没有关联:

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

repsonse中的更改将更改响应文本,但不会更改附加的DOM对象。因此,搜索u附加的dom元素,并在那里进行

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