开发者

probably stupid bug in code

What reason this code works perfectly:

<script type="text/javascript">
        $(document).ready(function() {
            $("#login_form").submit(function() {
                $.post("ajax_login.php", {
                    nam: $('#nam').val(),
                    mPassword: $('#mPassword').val()
                }, function(data) {
                    if (data == 'yes') {
                        $("#msgbox").fadeTo(200, 0.1, function() {
                            $(this).html('Log').addClass('messageboxok').fadeTo(900, 1, function() {
                                document.location = 'secure.php';
                            });
                        });
                    } else {
                        $("#msgbox").fadeTo(200, 0.1, function() {
                            $(this).html('Login inválido').addClass('messageboxerror').fadeTo(900, 1);
                        });
                    }
                });
                return false;
            });
        });
    </script>

and this code doesn't work? (none message is showed) it have any bug?

<script type="text/javascript">
    $(document).ready(function() {
        $("#login_form").submit(function() {
            $.post("ajax_login.php", {
                nam: $('#nam').val(),
                mPassword: $('#mPassword').val()
            }, function(data) {
                if (data == 'yes') {
                    $("#msgbox").fadeTo(200, 0.1, function() {
                        $(this).html('Log').addClass('messageboxerror').fad开发者_开发问答eTo(900, 1);
                    });
                } else {
                    $("#msgbox").fadeTo(200, 0.1, function() {
                        $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900, 1, function() {
                            document.location = 'secure.php';
                        });
                    });
                }
            });
            return false;
        });

    });
</script>

edit: two scripts works

the question is related to the second example. What is the problem?


Looks to me that you prematurely closed the if statement in your second code sample. It now looks like the else applies to the function and not the if.

Edit: There seems to still be a problem with some placement in your code (namely the return false statement in the second code). Try this:

<script type="text/javascript">
    $(document).ready(function() {
        $("#login_form").submit(function() {
            $.post("ajax_login.php", {
                nam: $('#nam').val(),
                mPassword: $('#mPassword').val()
            }, function(data) {
                if (data == 'yes') {
                    $("#msgbox").fadeTo(200, 0.1, function() {
                        $(this).html('Log').addClass('messageboxerror').fadeTo(900, 1);
                    });
                } else {
                    $("#msgbox").fadeTo(200, 0.1, function() {
                        $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900, 1, function() {
                            document.location = 'secure.php';
                        }); 
                    });
                }
            });
            return false;
        });
    });
</script>


In the second function you have:

function(data) {
  ...
} else {
  ...
}

You can't have an else clause on a function. It seems that you have some ending brackets in the wrong place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜