通知仅适用于第一次 - AJAX

时间:2015-02-03 11:02:16

标签: javascript jquery ajax fadeout

我创建了一个登录系统。通过AJAX提交登录表单。

然而,响应只是第一次显示。即。假设我先输入错误的凭证。回应显示。但是,当我再次输入新凭据时,它不会显示响应消息。对不起,我对JQuery比较陌生。

下面的Gven是Jquery代码

<script type="text/javascript">
    $(document).ready(function() {
        var form = $('#loginform');
        form.submit(function (ev) {
        ev.preventDefault();
        $.ajax({
            type: form.attr('method'),
            url: form.attr('action'),
            cache: false,
            data: form.serialize(),
            success: function (data) {
                if(data == 1){
                    $("#user-result").html("<font color ='#006600'> Logged in | Redirecting..</font>");
                    setTimeout(
                    function() 
                    {
                        window.location.replace("index.php");
                    }, 1000);
                }
                else{
                    $("#user-result").html(data).delay( 2500 ).fadeOut( 800 );
                }
           }
        });        
    });
});
</script>

我认为这可能是Fadeout的问题。任何帮助?

4 个答案:

答案 0 :(得分:2)

由于您使用的是fadeOut(),因此在第二个通知到来时隐藏user-result元素。所以

$("#user-result").html("<font color ='#006600'> Logged in | Redirecting..</font>").show();
$("#user-result").html(data).show().delay( 2500 ).fadeOut( 800 );

答案 1 :(得分:1)

是的,fadeout隐藏邮件容器的问题。在显示新消息之前,您必须显示容器:

$("#user-result").html("<font color ='#006600'> Logged in | Redirecting..</font>").show();

答案 2 :(得分:0)

一旦任何元素淡出,它将不会出现,直到页面重新加载或fadeIn()

                    if(data == 1){
                        $("#user-result").html("<font color ='#006600'> Logged in | Redirecting..</font>").fadeIn("slow");

                        setTimeout(
                              function() 
                              {
                                window.location.replace("index.php");
                              }, 1000);

                    }
                    else
                    {
                        $("#user-result").html(data).delay( 2500 ).fadeOut( 800 );
                    }

当data == 1时,使用fadeIn()

答案 3 :(得分:0)

你需要使用setInterval(function(){});为了每次循环。