0

在这些代码中,我试图提交表单,username='admin'提交password='password'后它将进入编辑页面。如果用户想要更改username并且password它可以在此处更改,但我的问题是我必须在我的代码中进行哪些更改,以便它可以读取在 and 中所做的更改(如果有)usernamepassword并继续登录。第一次应该使用凭据登录,admin and password如果用户更改,那么我必须做什么

$(document).unbind('pageinit').bind('pageinit', function () {          
        $("#button").click(function () {
             callXMLConnection();
        });
        $("#button1").click(function () {
             callConnection();
        }); 
});

function callXMLConnection(){
    var u1 = $("#textusername").val();
    var p1 = $("#textpassword").val();
    if(u1 == 'admin' && p1 == 'password'){
        $.mobile.changePage("#page1");
        $("#textusername1").val(u1);
        $("#textpassword1").val(p1);
    }else{
        alert("username and password is not correct");
    }
}
function callConnection(){
    localStorage.setItem("user", $("#textusername1").val());
    localStorage.setItem("pass", $("#textpassword1").val());
    localStorage.setItem("auth", $("#textauthentication").val());
    if(localStorage.getItem("user") == "" && localStorage.getItem("pass") == ""){
        alert("enter the username and password");
    }else{
        $.mobile.changePage("#page2");
    }
}
4

1 回答 1

0

您将新凭据存储在 localStorage 中,因此在登录期间检查它们是否存在并检索它们,如果不存在则使用默认值:

function callXMLConnection(){
    var u1 = $("#textusername").val();
    var p1 = $("#textpassword").val();

    //Check for stored credentials and use defaults if not found
    var user = localStorage.getItem("user") ? localStorage.getItem("user") : 'admin';
    var password = localStorage.getItem("password") ? localStorage.getItem("password") : 'password';

    if(u1 == user && p1 === password){ 
         $.mobile.changePage("#page1");
         $("#textusername1").val(u1);
         $("#textpassword1").val(p1);            
    }else{
      alert("username and password is not correct");
    }
}
于 2013-07-16T06:11:21.353 回答