带有注释系统的Ajax帖子发布
大约2年前,我下载了一个php/ajax评论脚本,从第一天起就遇到了这个小问题。我试着联系作者,但找不到他 有时,当我单击“发送”按钮时,该按钮保持禁用状态,并且什么也没有发生。它只是继续显示动画繁忙的图片。我根本没有收到任何错误消息 我想知道你们这些专业人士能不能帮我 以下是相关代码:带有注释系统的Ajax帖子发布,ajax,forms,Ajax,Forms,大约2年前,我下载了一个php/ajax评论脚本,从第一天起就遇到了这个小问题。我试着联系作者,但找不到他 有时,当我单击“发送”按钮时,该按钮保持禁用状态,并且什么也没有发生。它只是继续显示动画繁忙的图片。我根本没有收到任何错误消息 我想知道你们这些专业人士能不能帮我 以下是相关代码: <div class="comment_heading">Leave a Comment</div> <div class="post_comment"> <texta
<div class="comment_heading">Leave a Comment</div>
<div class="post_comment">
<textarea name="txtpostcomment" id="txtpostcomment-'.$postid.'" class="txtpostcomment"></textarea>
<button class="btnpostcomment" id="btnpostcomment-'.$postid.'" onclick="comment('.$postid.');" type="button">Send</button>
<input type="hidden" name="token" id="token" value="'.$_SESSION['token'].'">
<script>document.getElementById("txtpostcomment-'.$postid.'").focus();</script>
</div>
comment = function(postid1)
{
var txt = $('#txtpostcomment-'+postid1);
var btn = $('#btnpostcomment-'+postid1);
var comment1 = $(txt).val();
var token = $("#token").val();
$(btn).css('background-image', 'url(/comments/submit-busy.gif)');
$(btn).attr('disabled', true);
$.post("/comments/submit.php",{commenting:1, postid:postid1, comment: comment1, name: name, token: token},
function(msg)
{
if(msg.status)
{
$('.post_comment .error_msg').remove();
$('.comment-list-'+postid1).prepend(msg.html);
$(txt).val('');
$('.comChars').empty();
}
else
{
$('.post_comment .error_msg').remove();
$('.error_msg').clone().appendTo('.post_comment');
$('.error_msg:last').append(msg.error);
}
$(btn).css('background-image', 'none');
$(btn).attr('disabled', false);
$(txt).attr('disabled', false);
},'json');
}
留言
发送
document.getElementById(“txtpostcomment-'.$postid.”)focus();
注释=功能(1)
{
var txt=$('#txtpostcomment-'+postD1);
var btn=$(“#btnpostcomment-”+postid1);
var comment1=$(txt.val();
var token=$(“#token”).val();
$(btn.css('background-image','url(/comments/submit busy.gif));
$(btn).attr('disabled',true);
$.post(“/comments/submit.php”,{commenting:1,postid:postid1,comment:comment1,name:name,token:token},
功能(msg)
{
如果(消息状态)
{
$('.post_comment.error_msg').remove();
$('.comment list-'+postd1).prepend(msg.html);
$(txt).val(“”);
$('.comChars').empty();
}
其他的
{
$('.post_comment.error_msg').remove();
$('.error_msg').clone().appendTo('.post_comment');
$('.error\u msg:last').append(msg.error);
}
$(btn.css('background-image','none');
$(btn).attr('disabled',false);
$(txt).attr('disabled',false);
}“json”);
}
如果Ajax请求失败(由于网络断开连接、服务器错误等),代码似乎没有检查错误情况。检查javascript控制台是否存在任何js错误。由于它使用的是$.post
,如果您使用的jQuery版本支持它,您可能需要扩展代码以添加错误处理。或者,也可以使用$.ajax
。有关更多信息,请参阅或$。ajax is具有更多选项和灵活性