From 25233d09ff75628a215166e8e1abc439f925ba21 Mon Sep 17 00:00:00 2001 From: printempw Date: Wed, 3 Feb 2016 21:42:27 +0800 Subject: [PATCH] listen on Enter key press event --- assets/js/index.utils.js | 62 +++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/assets/js/index.utils.js b/assets/js/index.utils.js index a5e1a500..681a52ad 100644 --- a/assets/js/index.utils.js +++ b/assets/js/index.utils.js @@ -2,36 +2,27 @@ * @Author: prpr * @Date: 2016-01-21 13:55:44 * @Last Modified by: prpr -* @Last Modified time: 2016-02-03 20:38:41 +* @Last Modified time: 2016-02-03 21:40:41 */ 'use strict'; -$('#login').click(function(){ - $('[data-remodal-id=login-modal]').remodal().open(); -}) - -$('#register').click(function(){ - $('[data-remodal-id=register-modal]').remodal().open(); -}) - -// Login Button Click Event -$("body").on("click", "#login-button", function(){ +var login = function() { var uname = $("#uname").val(); var passwd = $("#passwd").val(); if (checkForm("login", uname, passwd)) { - $.ajax({ - type: "POST", - url: "ajax.php?action=login", - dataType: "json", - data: {"uname":uname,"passwd":passwd}, - beforeSend: function() { + $.ajax({ + type: "POST", + url: "ajax.php?action=login", + dataType: "json", + data: { "uname": uname, "passwd": passwd }, + beforeSend: function() { showMsg("alert-info", "Logging in..."); }, - success: function(json) { - if (json.errno == 0) { - docCookies.setItem("uname", uname, null, '/'); - docCookies.setItem("token", json.token, null, '/'); + success: function(json) { + if (json.errno == 0) { + docCookies.setItem("uname", uname, null, '/'); + docCookies.setItem("token", json.token, null, '/'); if ($("#keep").prop("checked")) { docCookies.setItem("uname", uname, 604800, '/'); // 设置长效 token (7天) @@ -43,13 +34,12 @@ $("body").on("click", "#login-button", function(){ showAlert(json.msg); showMsg('hide', ""); } - } - }); + } + }); } -}); +} -// Register Button Click Event -$("body").on("click", "#register-button", function(){ +var register = function() { var uname = $("#reg-uname").val(); var passwd = $("#reg-passwd").val(); if (checkForm("register", uname, passwd, $("#reg-passwd2").val())) { @@ -75,7 +65,7 @@ $("body").on("click", "#register-button", function(){ } }); } -}); +} function checkForm(type, uname, passwd, passwd2) { switch(type) { @@ -117,3 +107,21 @@ function checkForm(type, uname, passwd, passwd2) { return false; } } + +$('#login').click(function(){ + $('[data-remodal-id=login-modal]').remodal().open(); +}) + +$('#register').click(function(){ + $('[data-remodal-id=register-modal]').remodal().open(); +}) + +// Register Event +$("body").on("keypress", "[data-remodal-id=register-modal]", function(event){ + if (event.which == 13) register(); +}).on("click", "#register-button", register); + +// Login Event +$("body").on("keypress", "[data-remodal-id=login-modal]", function(event){ + if (event.which == 13) login(); +}).on("click", "#login-button", login);