gpt4 book ai didi

javascript - 处理 jquery ajax 请求的数据返回

转载 作者:行者123 更新时间:2023-11-30 06:41:55 24 4
gpt4 key购买 nike

所以目前的问题不在于 ajax 本身,而在于一旦我取回数据该怎么办。我在尝试将数据添加到动态内容的类中时遇到了一个巨大的问题。这是执行 ajax 请求的代码,ajax 再次按预期工作,但是当我尝试将返回的数据添加到前面时,它会将其添加到我网站上的每个帖子 ( http://readysetfail.com)。

                     /*
* This is the ajax
*/
$(".reply").on( "submit" , function(){
// Intercept the form submission
var formdata = $(this).serialize(); // Serialize all form data

// Post data to your PHP processing script
$.post( "/comment.php", formdata, function( data ) {
// Act upon the data returned, setting it to #success <div>
$(this).parent('.post_comment').children('.view_comments').prepend(data);
$('.hidden').hide();
});
return false;
});
/*
* End ajax
*/

抱歉,如果我解释得很糟糕,作为总结,这是我理想中想要的。用户点击发表评论,然后它会淡出他们在正确帖子上发表的评论。

非常感谢!

乔恩

我解决了!

这是一个简单的解决方案,我像这样存储了 jquery 变量然后能够导航 :)

$(".reply").on( "submit" , function(){
// Intercept the form submission
var formdata = $(this).serialize(); // Serialize all form data
var reply = $(this);

// Post data to your PHP processing script
$.post( "/comment.php", formdata, function( data ) {
// Act upon the data returned, setting it to .success <div>
reply.parent().next().prepend(data);
$('.hidden').hide();
});
return false;
});

最佳答案

$(".reply").on("submit" , function(e) {
e.preventDefault();
var formdata = $(this).serialize();
$.post( "/comment.php", formdata, function( data ) {
$(this).siblings('.view_comments').prepend(data);
$('.hidden').hide();
});
});

关于javascript - 处理 jquery ajax 请求的数据返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10559411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com