0

クライアント側の Web アプリケーションの sessionStorage を使用しています。index.html に、jQuery を使用して sessionStorage 変数を設定します。

$.ajax({
        type: "POST",
        url: "login.php",
        data: "user="+$("#user_name").val()+"&password="+$("#password").val(),
            success: function(html){
                if(html=='true') {
                    $("#add_err").html("Correct. Loading...")               
                    sessionStorage.setItem('sessionUser',$("#user_name").val());
                    sessionStorage.setItem('sessionPassword',$("#password").val());
                    setTimeout(function(){
                        $(location).attr('href','eval.html');
                    },2000);
                } else {
                    $("#add_err").html("Incorrect");
                }
            },
        });

Ok。できます。eval.html で、アラート alert(sessionStorage.getItem('sessionUser')); を起動します。ユーザーを表示します。

アプリケーションを切断したい場合は、ボタン id=btnLogout をクリックします。

$("#btnLogout").click(function(){
      sessionStorage.clear();
      window.location.assign('index.html');
});

しかし、sessionStorage は何もクリアしません。別のアラートを index.html に配置しましたが、sessionUser は削除されません。

私は間違っていることを知りません。

index.html = login.js からのログイン ボタン:

$(document).ready(function(){
    $("#login").click(function(){
        $.ajax({
            type: "POST",
            url: "login.php",
            data: "username="+$("#user_name").val()+"&password="+$("#password").val(),
            success: function(html){
                if(html=='true') {
                    $("#add_err").html("User correct. Loading application...")              
                    sessionStorage.setItem('sessionUser',$("#user_name").val());
                    sessionStorage.setItem('sessionPassword',$("#password").val());
                    setTimeout(function(){
                        $(location).attr('href','eval.html');
                    },2000);
                } else {
                    $("#add_err").html("Incorrect");
                }
            },
        });
    });
}); 

eval.html からのログアウト ボタン:

$(document).ready(function(){
    $("#btnLogout").click(function(){
        sessionStorage.removeItem('sessionUser');
        sessionStorage.removeItem('sessionPassword');
        window.location.assign('index.html');
    });
});
4

2 に答える 2

0

問題が見つかりました。

すべてをもう一度見直してみると、クリックイベントが正しく配置され$(document).readyておらず、機能しませんでした。今では正しい位置にあり、正常に動作しています!!!!! ありがとう!!!

于 2015-03-26T15:43:43.937 に答える