HTML+jQuery實現簡單的登錄頁面

簡介

本文用示例展示簡單的登錄頁面的寫法。

會包括如下幾種方案:純HTML、HTML+jQuery(form data)格式、HTML+jQuery(json)格式。

公共代碼(後端接口)

用SpringBoot寫一個最簡單的登錄接口。

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風格:返回JSON
@RestController
public class LoginController {
    @PostMapping("login")
    public LoginVO login() {
        //省略對用戶名和密碼的判斷
        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

結果

示例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表單裡面的數據傳到後臺
                //dataType: "json", // 指定後臺傳過來的數據是json格式
                success: function (result) {
                    if (!result.success) {
                        $("#errormessage").text("用戶名或密碼錯誤");
                    } 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.點擊登錄

4.點擊確定

示例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",
            //發送給後端的數據
            {
                "userName": $(".username").val(),
                "password": $(".password").val()
            },
            //回調函數
            function (result) {
                if (!result.success) {
                    $("#errormessage").text("用戶名或密碼錯誤");
                } 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>

測試

測試結果和前邊“示例2:HTML+jQuery(form data)”一樣

1.訪問login.html

2.輸入用戶名和密碼

用戶名:輸入abc;密碼:輸入 1234

3.點擊登錄

4.點擊確定

到此這篇關於HTML+jQuery實現簡單的登錄頁面的文章就介紹到這瞭,更多相關HTML jQuery 登錄頁面內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: