0

在我的 wordpress 网站中,我有一个标准的评论提交表单。我已经向它添加了一个脚本(ajax),所以我不必重新加载页面来提交评论。一切正常,但如何在不刷新页面的情况下显示提交的评论?

我的代码:

jQuery('document').ready(function($){  
var commentform=$('form#commentform');  
commentform.prepend('<div id="wdpajax-info" ></div>');  
var infodiv=$('#wdpajax-info');  
commentform.validate({  
    submitHandler: function(form){  
        var formdata=commentform.serialize();  
        infodiv.html('<p>Proszę czekać...</p>');  
        var formurl=commentform.attr('action');  
        $.ajax({  
            type: 'post',  
            url: formurl,  
            data: formdata,  
            error: function(XMLHttpRequest, textStatus, errorThrown){  
                infodiv.html('<p class="wdpajax-error" >Sprawdź wszystkie pola!</p>');  
            },  
            success: function(data, textStatus, html){
                $("ul.commentlist").append(formdata);
                $("ul.commentlist li:last").fadeIn("slow");
                if(data=="success")  
                    infodiv.html('<p class="wdpajax-success" >Dzięki, komentarz został dodany!</p>');  
                else  
                    infodiv.html('<p class="wdpajax-error" >Błąd</p>');  
                commentform.find('textarea[name=comment]').val('');  
            }  
        });  
    }  
  });  
});
4

1 回答 1

1

I would recommend refreshing the page so the user can see that the comment has really been saved on the server side. If you still want to do it without refreshing the page you should add a new div with the comment. Something similar to this:

success: function(data, textStatus, html){
            $("ul.commentlist").append(formdata);
            $("ul.commentlist li:last").fadeIn("slow");
            if(data=="success")  {
              infodiv.html('<p class="wdpajax-success" >Dzięki, komentarz został dodany!</p>');  
              // you should inicialize commentdiv with the selector of the part of the page you want to show the comment
              commentdiv.html('<p class="comment">'+formdata+'</p>');
}
于 2013-09-22T22:40:43.820 回答