记住密码

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>login-AM</title>
    <script src="js/jquery-2.1.4.js"></script>
    <script src="js/jquery.cookie.js"></script>
    <script>
        $(document).ready(function () {
            var rem = $.cookie('remember');
            if (rem) {
                $("#remember").prop("checked", true);
                $("#username").val($.cookie("username"));
                $("#password").val($.cookie("password"));
            }
        });
    </script>
</head>
<body>
<div class="container">
    <div class="form row">
        <div class="form-horizontal col-md-offset-3" id="login_form">
            <h3 class="form-title">LOGIN</h3>
            <div class="col-md-9">
                <div class="form-group">
                    <i class="fa fa-user fa-lg"></i>
                    <input class="form-control required" type="text" placeholder="Username" id="phone" name="username"
                           autofocus="autofocus" maxlength="20"/>
                </div>
                <div class="form-group">
                    <i class="fa fa-lock fa-lg"></i>
                    <input class="form-control required" type="password" placeholder="Password" id="password"
                           name="password" maxlength="8"/>
                </div>
                <div class="form-group">
                    <label class="checkbox">
                        <input type="checkbox" id="remember" value="1"/>记住我
                    </label>

                </div>
                <div class="form-group col-md-offset-9">
                    <a type="button" class="btn btn-primary pull-right" id="register" href="/register/">注册</a>
                    <button type="button" class="btn btn-success pull-right" id="submit">登录</button>
                </div>
                <div class="form-group">
                    <i class="fa fa-exclamation-triangle fa-lg" id="display_text"></i>
                </div>
            </div>
        </div>
    </div>
</div>
<script>

    $("#submit").click(function () {
        if ($("#phone").val() != "" && $("#password").val() != "") {
            save_cookies();
            $.ajax({
                url: 'http://192.168.2.149/clickfarming-web/login',
                type: 'post',
                data: {
                    'phone': $("#phone").val(),
                    'password': $("#password").val()
                },
                dataType: 'json',
                success: function (args) {
                    if (args.res == 1) {
                        // window.location="/index/";
                    } else {
                        $("#display_text").html("用户名或密码错误.");
                    }
                },
                error: function (data) {
                    alert('ajax error');
                }
            });
        } else {
            alert("用户名或密码不能为空。");
        }
    });

    //保存到cookie
    function save_cookies() {
        if ($("#remember").prop("checked")) {
            var phone = $("#phone").val();
            var password = $("#password").val();

            $.cookie("remember", "true", {expires: 7});
            $.cookie("phone", phone, {expires: 7});
            $.cookie("password", password, {expires: 7});
        } else {
            $.cookie("remember", "false", {expires: -1});
            $.cookie("phone", "", {expires: -1});
            $.cookie("password", "", {expires: -1});
        }
    };


</script>


</body>
</html>

记住密码的demo!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值