简体   繁体   中英

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. 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:

$('#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()... .

It doesn't matter that you call logger() again and the if statement is false the second time around. If you want to disable these callbacks you have to do it explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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