zoukankan      html  css  js  c++  java
  • 记住用户名 缓存案例 localstorage

     案例代码

    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>

    <body>
        <input type="text" id="username"> <br>
        <input type="checkbox" name="" id="remember"> 记住用户名

        <script>
            var username = document.querySelector('#username');
            var remember = document.querySelector('#remember');

            if (localStorage.getItem('username')) {
                username.value = localStorage.getItem('username');
                remember.checked = true;
            }

            remember.addEventListener('change', function() {
                if (this.checked) {
                    if (username.value == '') {
                        alert('请先填写用户名...');
                        this.checked = false;
                        return false;
                    }
                    localStorage.setItem('username', username.value);
                } else {
                    localStorage.removeItem('username');
                }
            })
        </script>
    </body>

    </html>
  • 相关阅读:
    [CF1263E] Editor
    [CF1288D] Minimax Problem
    [CF1294E] Obtain a Permutation
    [CF770C] Online Courses In BSU
    [CF832D] Misha, Grisha and Underground
    [CF917B] MADMAX
    [CF938D] Buy a Ticket
    [CF959E] Mahmoud and Ehab and the xor-MST
    [CF999E] Reachability from the Capital
    [CF960F] Pathwalks
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/13064288.html
Copyright © 2011-2022 走看看