zoukankan      html  css  js  c++  java
  • 使用JS获取SessionStorage的值

    参考:https://www.jb51.net/article/132729.htm

    获取sessionStorage的意义

    首先获取它是为了将获得的信息输出或者alert();让人容易看到,

    其次,在静态页面中,如果使用sessionStorage就相当于在动态页面里连接了数据库一样

    例如:我上一篇所做的为button按钮添加回车事件的项目中所用到的可以使用js中的sessionStorage获取页面输入的信息,也可以获得后台计算所得的数据,并且显示出来。

    废话不多说,看代码重要:

    具体实现

    <script type="text/javascript">
        function login(){
          var username=window.document.getElementById("username").value;
          var password=window.document.getElementById("password").value;
          if(password=="123456")
          {
            window.sessionStorage.setItem("username", username);
            window.location.href="../index1.html" rel="external nofollow" ;
          }else{
            alert("密码错误请输入正确的密码,例如:123456!");
            return false;
          }
        }
    </script>
    <input type="text" id="username" class="11" placeholder="请输入真实姓名"/>
    <input type="password" id="password" placeholder="请输入密码(默认密码123456)"/>
    <input type="button" value="登录考试" onclick="login()">
    

    以上代码是获取username的值并传到下一个界面

    并且获得password的值与事先设置好的对比,不同就是错误 就不可以登录

    <script>
       $(function () {
         var username= window.sessionStorage.getItem("username")
         $("#yhm").html("登录用户:"+username)
         $("#name").html(username)
         if(window.sessionStorage.getItem("username")===null){
           alert("您还没有登录,请登录后重试!")
           window.location.href="Pages/index.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;
         }
         $("#esc").on("click",function(){
           window.sessionStorage.clear();
           window.location.href="Pages/index.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;
         });
      })
     </script>
    

    获取前段页面输入的值并且显示至对应的位置

    <div id="yhm"></div>
    

    并且判断是否有sessionStorage的值,如果没有,自动返回登录页面,并做出相应的提示

    点击事件触发后清空sessionStorage的值

    <script>
    $("#esc").on("click",function(){
           window.sessionStorage.clear();
           window.location.href="Pages/index.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;
         });
    </script>
    

    当点击id为esc的按钮时触发clear事件

    <button id="esc" style="background-color: #FF0000">退出考试系统</button>
    

    则自动返回登录界面

  • 相关阅读:
    HDOJ2010_水仙花数
    npm报错:无法加载文件 D: odejs ode_globalwebpack.ps1,因为在此系统上禁止运行脚本
    百度全景地图使用时提示flash版本过低 如何处理?
    寂静之声 的歌词的中文意思
    国产电脑操作系统有哪些?
    解决mac系统中npm全局安装提示没有权限的问题
    nodemon运行 提示错误:无法加载文件 C:UsersgxfAppDataRoaming pm odemon.ps1。
    git设置忽略文件及目录
    Element中的Cascader 级联选择器高度不能正常显示如何解决2
    wepy内网环境下进行项目初始化异常处理(Failed to download repo standard: read ECONNRESET)
  • 原文地址:https://www.cnblogs.com/yingyigongzi/p/11049551.html
Copyright © 2011-2022 走看看