<div class="container" style="margin-top: 300px; "> <div class="row"> <div class="col-md-offset-4 col-md-4 "> <p><input class="sign_in_input" id="username" placeholder="请输入用户名" autofocus></p> <p><input class="sign_in_input" id="password" type="password" placeholder="请输入密码" ></p> <button id='sign_in_button' class="btn btn-lg btn-warning btn-block" style="opacity: 0.9" onclick="sign_in()">登 录</button> </div> </div> </div> <script> //写方法提交用户名和密码 function sign_in(){ //获取用户名 var username = $('#username').val() //获取密码 var password = $('#password').val() //拼接url var url = '/checkuser/username='+username+'/password='+password; $.getJSON(url,function(msg){ if (msg.result == 0){ alert("用户名或密码错误") } else{ // alert('登陆成功') //存储账号 信息,设置生命周期 //获取当前时间 var date = new Date() //getTime()是方法要加() date.setTime(date.getTime() + 1000*60*60) // console.log(date) $.cookie('username',username,{expires:date}) window.location.href = "/" } }) }; //监听回车事件 //event参数是键盘监听事件 $(document).keyup(function(event){ //判断回车键的code if (event.keyCode == 13){ //如果确定点击了回车键,那么就调用方法 // sign_in(); //模拟点击按钮 $('#sign_in_button').click(); } }); </script>