HTML+jQuery實(shí)現(xiàn)簡單的登錄頁面
簡介
本文用示例展示簡單的登錄頁面的寫法。
會(huì)包括如下幾種方案:純HTML、HTML+jQuery(form data)格式、HTML+jQuery(json)格式。
公共代碼(后端接口)
用SpringBoot寫一個(gè)最簡單的登錄接口。
Controller
package com.example.controller;
import com.example.entity.LoginVO;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
//跨域
@CrossOrigin
//Rest風(fēng)格:返回JSON
@RestController
public class LoginController {
@PostMapping("login")
public LoginVO login() {
//省略對(duì)用戶名和密碼的判斷
LoginVO loginVO = new LoginVO();
loginVO.setSuccess(true);
loginVO.setData("This is data");
return loginVO;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo_SpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo_SpringBoot</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
示例1:最簡(純HTML)
代碼
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登錄頁</title>
</head>
<body>
<form action="http://localhost:8080/login" method="post">
<label for="username">用戶名:</label>
<input type="text" name="username" id="username">
<label for="password">密碼:</label>
<input type="password" name="password" id="password">
<!--下邊這樣寫也可以
<label for="username">
用戶名:<input type="text" name="username" id="username">
</label>
<label for="password">
密碼:<input type="password" name="password" id="password">
</label>-->
<button type="submit">登錄</button>
</form>
</body>
</html>
測試
1.訪問login.html

2.輸入用戶名和密碼
用戶名:輸入abc;密碼:輸入 1234
結(jié)果

示例2:HTML+jQuery(form data)
代碼
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登錄頁</title>
<script src="https://cdn.staticfile.org/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<form id="login-form">
<label for="username">用戶名:</label>
<input type="text" name="username" id="username">
<label for="password">密碼:</label>
<input type="password" name="password" id="password">
</form>
<div id="error-message"></div>
<button type="submit" onclick="loginViaFormData()">登錄</button>
<script>
function loginViaFormData() {
$.ajax(
{
type: "post",
url: "http://localhost:8080/login",
data: $("#login-form").serialize(), // 序列化form表單里面的數(shù)據(jù)傳到后臺(tái)
//dataType: "json", // 指定后臺(tái)傳過來的數(shù)據(jù)是json格式
success: function (result) {
if (!result.success) {
$("#errormessage").text("用戶名或密碼錯(cuò)誤");
} else if (result.success) {
alert("登錄成功");
// 跳到index.html頁面
window.location.href="index.html" rel="external nofollow" rel="external nofollow" ;
}
}
}
)
}
</script>
</body>
</html>
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>This is title</title>
</head>
<body>
<div class="container">
登錄成功后的頁面
</div>
<script>
</script>
</body>
</html>
測試
1.訪問login.html

2.輸入用戶名和密碼
用戶名:輸入abc;密碼:輸入 1234

3.點(diǎn)擊登錄

4.點(diǎn)擊確定

示例3:HTML+jQuery(json)
代碼
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登錄頁</title>
<script src="https://cdn.staticfile.org/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<form id="login-form">
<label for="username">用戶名:</label>
<input type="text" name="username" id="username">
<label for="password">密碼:</label>
<input type="password" name="password" id="password">
</form>
<div id="error-message"></div>
<button type="submit" onclick="loginViaJson()">登錄</button>
<script>
function loginViaJson() {
$.post("http://localhost:8080/login",
//發(fā)送給后端的數(shù)據(jù)
{
"userName": $(".username").val(),
"password": $(".password").val()
},
//回調(diào)函數(shù)
function (result) {
if (!result.success) {
$("#errormessage").text("用戶名或密碼錯(cuò)誤");
} else if (result.success) {
alert("登錄成功");
// 跳到index.html頁面
window.location.href="index.html" rel="external nofollow" rel="external nofollow" ;
}
}
)
}
</script>
</body>
</html>
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>This is title</title>
</head>
<body>
<div class="container">
登錄成功后的頁面
</div>
<script>
</script>
</body>
</html>
測試
測試結(jié)果和前邊“示例2:HTML+jQuery(form data)”一樣
1.訪問login.html

2.輸入用戶名和密碼
用戶名:輸入abc;密碼:輸入 1234

3.點(diǎn)擊登錄

4.點(diǎn)擊確定

到此這篇關(guān)于HTML+jQuery實(shí)現(xiàn)簡單的登錄頁面的文章就介紹到這了,更多相關(guān)HTML jQuery 登錄頁面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jquery修改屬性值實(shí)例代碼(設(shè)置屬性值)
jQuery attr()方法用于設(shè)置/改變屬性值,下面的例子演示如何改變(設(shè)置)鏈接中 href 屬性的值2014-01-01
jquery入門—編寫一個(gè)導(dǎo)航條(可伸縮)
編寫一個(gè)導(dǎo)航條,單擊標(biāo)題時(shí),可以伸縮導(dǎo)航條內(nèi)容,簡化內(nèi)容或顯示更多內(nèi)容等等效果相當(dāng)不錯(cuò),感興趣的朋友可以了解下哦2013-01-01
jQuery模擬html下拉多選框的原生實(shí)現(xiàn)方法示例
這篇文章主要介紹了jQuery模擬html下拉多選框的原生實(shí)現(xiàn)方法,結(jié)合完整實(shí)例形式分析了jQuery動(dòng)態(tài)操作頁面元素實(shí)現(xiàn)select下拉框效果的相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
jquery DataTable實(shí)現(xiàn)前后臺(tái)動(dòng)態(tài)分頁
本篇文章主要介紹了jquery DataTable實(shí)現(xiàn)前后臺(tái)動(dòng)態(tài)分頁。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
jquery使用ul模擬select實(shí)現(xiàn)表單美化的方法
這篇文章主要介紹了jquery使用ul模擬select實(shí)現(xiàn)表單美化的方法,涉及jquery鼠標(biāo)事件及頁面元素樣式的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
jquery實(shí)現(xiàn)彈出層登錄和全屏層注冊(cè)特效
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)彈出層登錄和全屏層注冊(cè)特效,推薦給大家,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-08-08
jQuery+canvas實(shí)現(xiàn)簡單的球體斜拋及顏色動(dòng)態(tài)變換效果
這篇文章主要介紹了jQuery+canvas實(shí)現(xiàn)簡單的球體斜拋及顏色動(dòng)態(tài)變換效果,通過jQuery+html5的canvas利用時(shí)間函數(shù)進(jìn)行實(shí)時(shí)數(shù)學(xué)運(yùn)算動(dòng)態(tài)繪制拋物線圖形的技巧,需要的朋友可以參考下2016-01-01

