Javascript 提交后显示评论

Javascript 提交后显示评论,javascript,jquery,ajax,wordpress,forms,Javascript,Jquery,Ajax,Wordpress,Forms,在我的wordpress网站上,我有一个标准的评论提交表单。我已经添加了一个脚本(ajax),所以我不必重新加载页面来提交评论。一切正常,但如何在不刷新页面的情况下显示提交的评论 我的代码: jQuery('document').ready(function($){ var commentform=$('form#commentform'); commentform.prepend('<div id="wdpajax-info" ></div>'); var

在我的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('');  
            }  
        });  
    }  
  });  
});
jQuery('document').ready(函数($){
var commentform=$('form#commentform');
注释格式。前置(“”);
var infodiv=$('wdpajax info');
commentform.validate({
submitHandler:函数(形式){
var formdata=commentform.serialize();
infodiv.html('Proszęczekać…

'); var formurl=commentform.attr('action'); $.ajax({ 键入:“post”, url:formurl, 数据:formdata, 错误:函数(XMLHttpRequest、textStatus、ErrorSprown){ html(“

Sprawdźwszystkie pola!

”; }, 成功:函数(数据、文本状态、html){ $(“ul.commentlist”).append(formdata); $(“ul.commentlist li:last”).fadeIn(“slow”); 如果(数据=“成功”) infodiv.html(“

Dzięki,komentarz zostałdodany!

”; 其他的 html(“

Błd

”; commentform.find('textarea[name=comment]').val(''; } }); } }); });
我建议刷新页面,以便用户可以看到评论确实保存在服务器端。 如果您仍然希望在不刷新页面的情况下执行此操作,则应添加一个带有注释的新div。类似于此:

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>');
}
success:函数(数据、文本状态、html){
$(“ul.commentlist”).append(formdata);
$(“ul.commentlist li:last”).fadeIn(“slow”);
如果(数据=“成功”){
infodiv.html(“

Dzięki,komentarz zostałdodany!

”; //您应该使用页面中要显示注释部分的选择器来取消commentdiv的标记 html('

'+formdata+'

'); }
在类似应用程序和AJAX驱动的网站时代,页面刷新不再是“嘿,我们保存了一些东西!”在我看来,在不刷新页面的情况下显示注释会更好。formdata向我显示:comment=comment&comment\u post\u ID=746&comment\u parent=0&\u wp\u unfiltered\u html\u comment=dcce3d07fc&akismet\u comment\u nonce=171be0e422