简体   繁体   English

如果语句不符合预期

[英]If Statement Not Acting As Expected

$(document).ready(function(){
    logger();
});
function logger()
    {
        if(localStorage.getItem("status") === null)
            {
                $("#test").html("Not logged in.");
                $("#buttonlogin").click(function(){
                    var ul = $("#userlogin").val();
                    var pl = $("#passlogin").val();
                    $.post("includes/logger.php", {type : "login", user : ul, pass : pl}, function(dlogin){
                        if(dlogin == 1)
                            {
                                $("#outlogin").html("Please enter a username.");
                                $("#userlogin").focus();
                            }
                        else if(dlogin == 2)
                            {
                                $("#outlogin").html("Please enter password.");
                                $("#passlogin").focus();
                            }
                        else if(dlogin == 3)
                            {
                                $("#outlogin").html("This username doesn't exist.");
                                $("#userlogin").focus();
                            }
                        else if(dlogin == 4)
                            {
                                $("#outlogin").html("This username and password don't match.");
                                $("#userlogin").focus();
                            }
                        else
                            {
                                localStorage.setItem("status", dlogin);
                                logger();
                            }
                    });
                });
                $("#buttonregister").click(function(){
                    var ur = $("#userregister").val();
                    var pr = $("#passregister").val();
                    var cpr = $("#confirmpassregister").val();
                    $.post("includes/logger.php", {type : "register", user : ur, pass : pr, cpass : cpr}, function(dregister){
                        if(dregister == 1)
                            {
                                $("#outregister").html("Please enter a username.");
                                $("#userregister").focus();
                            }
                        else if(dregister == 2)
                            {
                                $("#outregister").html("Please enter a password.");
                                $("#passregister").focus();
                            }
                        else if(deregister == 3)
                            {
                                $("#outregister").html("Please enter a confirm password.");
                                $("#cpassregister").focus();
                            }
                        else if(dregister == 4)
                            {
                                $("#outregister").html("Password and confirm password do not match.");
                                $("#passregister").focus();
                            }
                        else if(dregister == 5)
                            {
                                $("#outregister").html("This username is already taken.");
                                $("#userregister").focus();
                            }
                        else
                            {
                                localStorage.setItem("status", dregister);
                                logger();
                            }
                    });
                });
            }
        else
            {
                $("#test").html("You are logged in.");
                $("#buttonlogout").click(function(){
                    localStorage.removeItem("status");
                    logger();
                });
            }
    }

The above code is meant to check whether or not a localStorage variable is in existence or not. 上面的代码旨在检查localStorage变量是否存在。 If it is then only allow the log out button to be pressed. 如果是,则仅允许按下注销按钮。 If is doesn't then let the two forms to work. 如果不是,则让这两种形式起作用。 Once it is done with either it is supposed to recheck if the variable is set and then do as I said above. 一旦完成任一操作,就应该重新检查是否设置了变量,然后按照我上面所说的进行操作。 However it ignores it when a user logs in and allows the forms to run. 但是,当用户登录并允许表单运行时,它将忽略它。 If you refresh however it works fine. 如果您刷新,则可以正常工作。 I cannot for the life of me figure out why this is happening, and it is beginning to piss me off. 我无法为自己的一生弄清为什么会这样,而且它开始激怒我。 Any help would be appreciated. 任何帮助,将不胜感激。

On your else statement, try adding: 在您的else语句上,尝试添加:

$('#buttonlogin').unbind('click');
$('#buttonregister').unbind('click');

If I understand your problem correctly, what's happening is those events are registered when you first run $("#buttonlogin").click(function()... . 如果我正确地理解了您的问题,那是在您第一次运行$("#buttonlogin").click(function()...时注册了这些事件。

It doesn't matter that you call logger() again and the if statement is false the second time around. 再次调用logger()if语句第二次为false都没关系。 If you want to disable these callbacks you have to do it explicitly. 如果要禁用这些回调,则必须显式执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM