Javascript 如何在发布数据jquery ajax时设置加载图像,并将alert msg更改为div error?

Javascript 如何在发布数据jquery ajax时设置加载图像,并将alert msg更改为div error?,javascript,jquery,ajax,Javascript,Jquery,Ajax,如果注释字段为空,我想显示一个div,而不是显示警报。我该怎么做?我的另一个问题是,数据会在几秒钟后显示。在加载期间是否可以添加加载图像 <script type="text/javascript"> $(function () { // on post comment click $('.bt-add-com').click(function () { var theCom = $('.the-new-com'); var theN

如果注释字段为空,我想显示一个div,而不是显示警报。我该怎么做?我的另一个问题是,数据会在几秒钟后显示。在加载期间是否可以添加加载图像

<script type="text/javascript">
$(function () {
    // on post comment click 
    $('.bt-add-com').click(function () {
        var theCom = $('.the-new-com');
        var theName = $('#name-com');
        var theMail = $('#mail-com');

        if (!theCom.val()) {
            alert('You need to write a comment!');
        } else {
            $.ajax({
                type: "POST",
                url: "ajax/add-comment.php",
                data: 'act=add-com&id_post=' + <? php echo $id_post; ?> +'&name=' + theName.val() + '&email=' + theMail.val() + '&comment=' + theCom.val(),
                success: function (html) {
                    theCom.val('');
                    theMail.val('');
                    theName.val('');
                    $('.new-com-cnt').hide('fast', function () {
                        $('.new-com-bt').show('fast');
                        $('.new-com-bt').after(html);
                    })
                }
            });
        }
    });
});
</script>

$(函数(){
//在发表评论时,单击
$('.bt add com')。单击(函数(){
var theCom=$('.the new com');
var theName=$(“#name com”);
var theMail=$(“#邮件com”);
如果(!theCom.val()){
提醒('您需要写评论!');
}否则{
$.ajax({
类型:“POST”,
url:“ajax/add comment.php”,
数据:'act=add com&id_post='++'&name='+theName.val()+'&email='+theMail.val()+'&comment='+theCom.val(),
成功:函数(html){
货币价值(“”);
邮件号(“”);
名称val(“”);
$('.new com cnt').hide('fast',function(){
$('new.com bt').show('fast');
$('.new.com bt')。在(html)之后;
})
}
});
}
});
});

您的代码看起来很好,几乎达到了预期效果。为什么不在ajax调用之前显示lodaing图像,并在success回调中隐藏它呢?另外,对于注释,只需在要显示的div上使用$.show()即可。在ajax调用为…)之前,使用$(“#加载”).html(“”)$.ajax({…和…if(!theCom.val()){$.show('#error'))}?是的。您以前使用过JSFIDLE或plunker吗?您可以共享您的代码,以便其他人可以提供帮助。不,但thanxs需要帮助。